Avatar of Mick 3009
Mick 3009
 asked on

Java quiz program

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;
   
    for(int counter=1; counter<=3;counter++){
        question = 1+quiz.nextInt(3);
   
    System.out.println ("Welcome to the Quiz Game!");
 
       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!");}
}
}
}
Java

Avatar of undefined
Last Comment
Mick 3009

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jeffrey Dake

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
krakatoa

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!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes