// CSE 142, Winter 2007 // Author: Martson Limsteppkai // // This program asks the user to do multiplication problems // and counts how many the user got right. // Uses a constant for the maximum value. import java.util.*; public class MultTutor { public static final int MAX = 50; public static void main(String[] args) { introduction(); Scanner console = new Scanner(System.in); Random r = new Random(); int count = 0; // keep playing games until playRound returns false while (playRound(console, r)) { count++; } System.out.println("You got " + count + " right."); } // This method plays one round of the multiplication game // and returns true if the player was correct and false if incorrect. public static boolean playRound(Scanner console, Random r) { int num1 = r.nextInt(MAX) + 1; int num2 = r.nextInt(MAX) + 1; System.out.print(num1 + " * " + num2 + " = "); int guess = console.nextInt(); // check whether guess was right, then print/return appropriate response if (num1 * num2 == guess) { System.out.println("Correct!"); return true; } else { System.out.println("Incorrect; the correct answer was " + (num1 * num2)); return false; } } // Prints a welcome message to the user. public static void introduction() { System.out.println("This program helps you practice multiplication"); System.out.println("by asking you random multiplication questions"); System.out.println("with numbers ranging from 1 to " + MAX); System.out.println("and counting how many you solve correctly."); System.out.println(); } }