1import java.util.Scanner;
2
3public class ProgrammingEx5_2 {
4 public static void main(String[] args) {
5 final int NUMBER_OF_QUESTIONS = 10; // Number of questions
6 int correctCount = 0; // Count the number of correct answers
7 int count = 0; // Count the number of questions
8 long startTime = System.currentTimeMillis();
9 String output = ""; // output string is initially empty
10 Scanner input = new Scanner(System.in);
11
12 while (count < NUMBER_OF_QUESTIONS) {
13 // 1. Generate two random single-digit integers
14 int number1 = (int) (Math.random() * 16);
15 int number2 = (int) (Math.random() * 16);
16
17 // 3. Prompt the student to answer “What is number1 + number2?”
18 System.out.print("What is " + number1 + " + " + number2 + "? ");
19 int answer = input.nextInt();
20
21 // 4. Grade the answer and display the result
22 if (number1 + number2 == answer) {
23 System.out.println("You are correct!");
24 correctCount++;
25 } else
26 System.out.println("Your answer is wrong.\n" + number1 + " + "
27 + number2 + " should be " + (number1 + number2));
28
29 // Increase the count
30 count++;
31
32 output += "\n" + number1 + " + " + number2 + "=" + answer
33 + ((number1 + number2 == answer) ? " correct" : " wrong");
34 }
35
36 long endTime = System.currentTimeMillis();
37 long testTime = endTime - startTime;
38
39 System.out.println("Correct count is " + correctCount
40 + "\nTest time is " + testTime / 1000 + " seconds\n" + output);
41 }
42}
43