-
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
-
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
-
Jus to be picky.....
This 'loop' executes one time only:
Code:
for (int i = 0; i < 1; ++i) {
.
.
}
eschew obfuscation
-
 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks