In: Computer Science
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output.
Example Output
What is 7 * 6 ? 42
Correct. Nice work!
Want more questions y or n ? y
What is 8 * 5 ? 40
Correct. Nice work!
Want more questions y or n ? y
What is 5 * 5 ? 25
Correct. Nice work!
Want more questions y or n ? y
What is 8 * 9 ? 66
Incorrect. The product is 72
Want more questions y or n ? n
You scored 3 out of 4
import java.util.Random; import java.util.Scanner; public class MultiplicationQuestions { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Random rand = new Random(); int n1, n2, result, total = 0, correct = 0; char ch = 'y'; while(ch == 'y'){ n1 = 1 + rand.nextInt(9); n2 = 1 + rand.nextInt(9); System.out.print("What is "+n1+" * "+n2+" ? "); result = scan.nextInt(); if(result==n1*n2){ System.out.println("Correct. Nice work!"); correct++; } else{ System.out.println("Incorrect. The product is "+(n1*n2)); } System.out.print("Want more questions y or n ? "); ch = scan.next().charAt(0); total++; } System.out.println("You scored "+correct+" out of "+total); } }