// GuessNumber.java // A solution to CS 5JA assignment 5, part 1b, Winter 2009 // updated by cmc, 2/24/09 import java.util.Scanner; public class GuessNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); int choice = 0; final int EASY = 5, MEDIUM = 20, DIFFICULT = 1000; // show menu System.out.println("1. Easy (1-" + EASY + ")\n" + "2. Medium (1-" + MEDIUM + ")\n" + "3. Difficult (1-" + DIFFICULT + ")"); // get valid choice from user do { System.out.println("\nEnter 1, 2 or 3: "); if (input.hasNextInt()) choice = input.nextInt(); else input.next(); }while (choice < 1 || choice > 3); // create corresponding random pick object int max = EASY; if (choice == 2) max = MEDIUM; else if (choice == 3) max = DIFFICULT; RandomPick p = new RandomPick(1, max); // let the user try to guess the pick System.out.println("\nGuess what number I picked."); int g = 0; while ( !p.goodGuess(g) ) { if (input.hasNextInt()) { g = input.nextInt(); if (p.highGuess(g)) System.out.println("No - too high. Try again."); if (p.lowGuess(g)) System.out.println("No - too low. Try again."); } else { String s = input.next(); System.out.println(s + " is not a valid guess."); } } System.out.println("Yes - " + g + " is the number!"); } }