I have to create an application that creates a quiz. The quiz is supposed to be at least 5 questions long, with 4 choices for each multiple choice question. If the answer is wrong, it is to display a message saying that its wrong and to give the right answer, and if it is right to display a congratulatory message. At the end of the quiz it should display the number of incorrect and correct answers, and the percentage of correct answers.
The problem I'm having is how to move onto the next question, and what code to add in to calculate the number of correct/incorrect answers and the percentage.
This is my code so far:
I know it's not supposed to be that hard, but I'm kind of stuck. I imagine I should have a counter after each question that should increment depending on the answer?
Thanks in advance.
The problem I'm having is how to move onto the next question, and what code to add in to calculate the number of correct/incorrect answers and the percentage.
This is my code so far:
Code:
public class Quiz
{
public static void main(String[] args) throws Exception
{
char response='y';
int choice=0;
String order="";
//while(response=='y')
do
{
System.out.println("What is the capital of Pennsylvania?");
System.out.println("***************");
System.out.println("1. Philadelphia");
System.out.println("2. Harrisburgh");
System.out.println("3. Easton");
System.out.println("4. Pittsburgh");
System.out.print("Enter your answer ");
choice=System.in.read();
System.in.read(); System.in.read(); //dummy System.in.read() to take care of Enter keystroke
choice=choice-48;
switch(choice)
{
case 1: order +="Philadelphia ";
System.out.println("Sorry, that is not the correct answer");
System.out.println("The correct answer is Harrisburgh");
break;
case 2: order+="Harrisburgh ";
System.out.println("That is the correct answer!");
break;
case 3: order+="Easton ";
System.out.println("Sorry, that is not the correct answer");
System.out.println("The correct answer is Harrisburgh");
break;
case 4: order+="Pittsburgh ";
System.out.println("Sorry, that is not the correct answer");
System.out.println("The correct answer is Harrisburgh");
default: order+="";
}
System.out.println("Do you want to stop");
response=(char)System.in.read();
System.in.read(); System.in.read();
}while(response=='y');//close while
}
}
I know it's not supposed to be that hard, but I'm kind of stuck. I imagine I should have a counter after each question that should increment depending on the answer?
Thanks in advance.