// Helene Martin, CSE 142 // Prompts the user to do addition problems until // the user gets three wrong. // Extension ideas: // expressions tutor -- randomly pick an operator // ask the user whether they still want to play // problems get longer as the user makes more points import java.util.*; public class AdditionTutor { public static void main(String[] args) { Scanner console = new Scanner(System.in); Random r = new Random(); /* This is the pseudocode we worked from: while (wrong < 3) generate a math problem how many operands? (random) generate each operand with + figure out answer prompt the user did user get it right? yes: points!! no: wrong++ */ int points = 0; int wrong = 0; while (wrong < 3) { boolean win = playRound(r, console); if (win) { points++; } else { wrong++; } } System.out.println("You got " + points + " points"); } // Generates one addition problem, prompts the user, // returns whether the user got it right or not. public static boolean playRound(Random r, Scanner console) { int operands = r.nextInt(4) + 2; int operand = r.nextInt(10) + 1; int answer = operand; System.out.print(operand); for (int i = 1; i < operands; i++) { operand = r.nextInt(10) + 1; System.out.print(" + " + operand); answer += operand; } System.out.print(" = "); int user = console.nextInt(); if (user == answer) { return true; } else { System.out.println("Wrong! The answer was " + answer); return false; } } }