How to create a quiz with random questions using Java

Written by sue smith Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to create a quiz with random questions using Java
With Java you can build interactive applications such as quizzes with a minimal amount of coding. (Polka Dot/Polka Dot/Getty Images)

With the Java programming language, you can create interactive applications such as quizzes. To create a Java quiz, you need to model your quiz questions and answers, then present them to the user. Your application also needs to capture the user input and check it against the correct answers. How you choose to store the data and present it to the user will depend on the needs of your own project. However, the logic of creating a quiz with randomly selected questions remains broadly the same whatever the details of your requirements are. Creating a quiz application is an accessible task even for Java beginners.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Create the questions and answers for your quiz. You can store your quiz data in many possible ways, such as in a database or external file. If you choose either of these options you will need to read the data into your program. For a simpler approach you can "hard-code" the question and answer data in your program using lists, as in the following example code:

    List<String> questions = new LinkedList<String>(); List<String> answers = new LinkedList<String>();

  2. 2

    Instantiate the question and answer variables. Add your quiz questions and answers to the lists you created, as in the following example:

    questions.add("What is the capital of Egypt?"); answers.add("Cairo"); questions.add("What is the capital of Spain?"); answers.add("Madrid");

    Continue to add to the lists until you have all of your questions and answers entered. Add the following code to create a random number generator and variable to keep track of the number of quiz questions:

    Random rand = new Random(); int numQu=10;

    Alter the number of questions if necessary.

  3. 3

    Iterate through the questions using a loop. What you do inside the loop depends on how you want to display the questions and capture user input. The following sample code demonstrates using the standard console. You need "try" and "catch" blocks in case of input/output errors:

    try{ BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); } catch(Exception e){ System.err.println(e); e.printStackTrace(); }

    The "catch" block will output the details of any errors. Inside the "try" block, the code creates a Buffered Reader for user input. After that line you can loop through the questions, outputting a randomly selected one each time:

    while(!questions.isEmpty()){ int randQu = rand.nextInt(numQu); System.out.println(questions.remove(randQu)); }

    The code removes the question so that it is not asked again.

  4. 4

    Capture user input inside the loop you created to iterate through the questions. After writing the current question out to the console, read the user input as follows:

    String ans = read.readLine();

    Each time the loop iterates, the current question will be output, then whatever the user types will be read in.

  5. 5

    Check the user's answer. After the line in which you read the user input into the program, check it against the correct answer you stored in your answers list, writing out an appropriate message:

    if(ans.equalsIgnoreCase(answers.remove(randQu))) System.out.println("CORRECT!"); else System.out.println("INCORRECT!");

    Finally, decrement the number of questions each time the loop iterates:

    numQu--;

    The program will present each question, read in and check the answer then move onto the next question until it runs out of questions.

Tips and warnings

  • If you want your quiz program to use a GUI (Graphical User Interface), you can use Java's JFC and Swing code libraries.
  • If you choose to store your quiz data externally to the application, you will need to carry out exception handling and plenty of testing.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.