21.6k views
0 votes
Write a program 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.

User Naveen
by
4.6k points

1 Answer

4 votes

Answer:

The Java code is given below with appropriate variable names for better understanding

Step-by-step explanation:

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);

}

}

User Lotok
by
4.4k points