I am having problems with my Java program Quiz Game (code below). I am trying to do a random multiple choice question quiz but it does not work the way I wish to. The program should output three random questions at a time but at run time it is stopping after the first one. Can someone please help me out?
import java.util.Random;
public class Sample3{
public static void main (String args[])
{
Random quiz = new Random();
int question;
System.out.println ("The instructions are: you will be given one type of topic,");
System.out.println ("The topic will have 3 multiple choice questions");
System.out.println ("If you get one wrong you start from the beginning.");
System.out.println ("If you get them all right you will win");
if ((question==1)){
System.out.println ("question: How many players are there in 1 football team?");
System.out.println ("9");
System.out.println ("10");
System.out.println ("11");
}
int ans1=Keyboard.readInt();
if((ans1==11)){
System.out.println ("Correct");}
if ((question==2)){
System.out.println ("Question : Which of these teams play in the Premier League?");
System.out.println ("press 1 for Juventus");
System.out.println ("press 2 for Chelsea");
System.out.println ("press 3 for PSG");
}
int ans2=Keyboard.readInt();
if((ans2==2)){
System.out.println ("Correct");}
if ((question==3)){
System.out.println ("Question 3: How do you score?");
System.out.println ("press 1 if by kicking the ball into the net");
System.out.println ("press 2 if by throwing the ball into the net");
System.out.println ("press 3 if by kicking the ball outside the turf");
}
int ans3=Keyboard.readInt();
if ((ans3==1)){
System.out.println ("Correct!");}
}
}
}
Using a random approach like that which is in a finite for loop is the opposite of a guarantee that all three questions will be asked - which seems strange to say the least.
You need something like a while loop instead, which runs until three questions have been answered. You'll then also have to ensure that 3 *different* questions were answered, otherwise nothing will make sense.
Mick 3009
ASKER
Thank you for your comment. Program updated accordingly and problem solved!
You need something like a while loop instead, which runs until three questions have been answered. You'll then also have to ensure that 3 *different* questions were answered, otherwise nothing will make sense.