// Helene Martin, CSE 142 // Helps a user practice their addition of numbers 1 - 50. // The game ends after 3 wrong answers are given. // development strategy: // we started by getting a single game to work // we put that game in a method // we called that game in a while loop // we noticed we needed information from the method so returned points import java.util.*; public class AdditionGame { public static void main(String[] args) { Scanner console = new Scanner(System.in); Random rand = new Random(); int misses = 0; int points = 0; while (misses < 3) { int result = playGame(rand, console); if (result == 1) { points++; } else { misses++; } } System.out.println("You earned " + points + " total points."); } // Prompts the user with an adding problem with 2 - 5 operands // and returns the number of points received (0 or 1). public static int playGame(Random rand, Scanner console) { int operands = rand.nextInt(4) + 2; int answer = 0; for (int i = 0; i < operands - 1; i++) { int operand = rand.nextInt(50) + 1; answer += operand; System.out.print(operand + " + "); } int operand = rand.nextInt(50) + 1; answer += operand; System.out.print(operand); // for debugging: System.out.print(" = " + answer); System.out.print(" = "); int userAns = console.nextInt(); if (userAns == answer) { return 1; } else { System.out.println("Wrong! The answer was " + answer); return 0; } } }