We help IT Professionals succeed at work.
Get Started

Java quiz program

Mick 3009
Mick 3009 asked
Last Modified: 2018-06-15
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();
        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();
        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!");}
Watch Question
Senior Director of Technology
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE