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.
- With the Java programming language, you can create interactive applications such as quizzes.
- Creating a quiz application is an accessible task even for Java beginners.
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
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");
- List
questions = new LinkedList (); List answers = new LinkedList (); 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?
- ");
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.
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:
- Random rand = new Random(); int numQu=10; Alter the number of questions if necessary.
- Iterate through the questions using a loop.
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:
- The "catch" block will output the details of any errors.
- Inside the "try" block, the code creates a Buffered Reader for user input.
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.
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.
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!");
- List
questions = new LinkedList (); List answers = new LinkedList (); 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?
- ");
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.
TIP
If you want your quiz program to use a GUI (Graphical User Interface), you can use Java's JFC and Swing code libraries.
WARNING
If you choose to store your quiz data externally to the application, you will need to carry out exception handling and plenty of testing.