Help on Application, Adding Correct Answers HELP


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Help on Application, Adding Correct Answers HELP

  1. #1
    Join Date
    Mar 2005
    Location
    Airzona
    Posts
    7

    Help on Application, Adding Correct Answers HELP

    Ok i made a quiz in java in an appplication, this is what i have when they get a correct or incorrect answer

    for (int i = 0; i < 1; ++i)
    {
    System.out.print("\n" + allQ[i].getQuestion());
    System.out.print("\n" + allQ[i].getChoiceA());
    System.out.print("\n" + allQ[i].getChoiceB());
    System.out.print("\n" + allQ[i].getChoiceC());
    System.out.print("\n" + allQ[i].getChoiceD());
    char answer = getInput.getChar("\nEnter your answer ");
    if((answer == 'B') || (answer == 'b'))
    {
    System.out.print("\n Correct!\n");

    }
    else
    {
    System.out.print("\n Incorrect! Right answer is B\n");
    }
    }

    now i have that 10 times for 10 different questions now i need to have it give the user a point every time the get a correct answer and then at the end have it be like 5/10 if the got 5 right

    thanks

  2. #2
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    Code:
    int correct = 0;//keeps track of how many are correctly answered
    
    for (int i = 0; i < 1; ++i)
    {
    System.out.print("\n" + allQ[i].getQuestion());
    System.out.print("\n" + allQ[i].getChoiceA());
    System.out.print("\n" + allQ[i].getChoiceB());
    System.out.print("\n" + allQ[i].getChoiceC());
    System.out.print("\n" + allQ[i].getChoiceD());
    char answer = getInput.getChar("\nEnter your answer ");
    if((answer == 'B') || (answer == 'b'))
    {
    System.out.print("\n Correct!\n");
    
    }
    else
    {
    System.out.print("\n Incorrect! Right answer is B\n");
    correct++;
    }
    
    System.out.println("You answered " + Integer.toString(correct) + " out of 10 correctly");
    }
    That would work, but to reallly make this program better you should have three classes. A Quiz, Question, and AnswerList class. The Quiz would consist of an ArrayList of questions, you could have a methd to make new questions and a method to remove questions. A Question would be made up of a String that is the questions itself and an AnswerList along with the index of the correct answer in the AnswerList. The answer list would simply be an array of Strings. If you are doing this program for school and your instructor just wants you to do everything in the main (very procedural and C like) then i would stick with your origional. However, if your teacher also wants you to take a more Object Oriented approach then i would lean towards something along the lines of what i just described. Anyways good luck.
    Java has 99 problems but a pointer ain't one

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Jus to be picky.....
    This 'loop' executes one time only:
    Code:
    for (int i = 0; i < 1; ++i) {
      .
      .
    }
    eschew obfuscation

  4. #4
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    Quote Originally Posted by sjalle
    Jus to be picky.....
    This 'loop' executes one time only:
    Code:
    for (int i = 0; i < 1; ++i) {
      .
      .
    }
    Yea, he said he has that code in his project 10 times in a row....yikes!
    Java has 99 problems but a pointer ain't one

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center