// CSE 142, Summer 2008 (Helene Martin) // Gives the user multiplication problems to solve. // If the user gets one right, s/he is given another. // When the user gets it wrong, the program stops and reports the number of correct answers. import java.util.*; public class MultTutor { public static void main(String[] args) { Random r = new Random(); Scanner console = new Scanner(System.in); int count = 0; while(askQuestion(r, console)) { count++; } System.out.println("You solved " + count + " correctly."); } // Asks the user one multiplication problem // returns whether or not they get it right public static boolean askQuestion(Random r, Scanner console) { int num1 = r.nextInt(20) + 1; int num2 = r.nextInt(20) + 1; int product = num1 * num2; System.out.print(num1 + " * " + num2 + " = "); int ans = console.nextInt(); if(ans == product) { System.out.println("Correct!"); return true; } else { System.out.println("Incorrect; the answer was " + product); return false; } } }