/** This program simulates a guessing game. The computer will randomly * think of some number we are to guess. We'll get a certain number of * tries to get it right. Program features a loop and random number * generation. */ import java.io.*; public class Driver { public static void main(String [] args) throws IOException { BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Welcome to my guessing game. Let's see if you can"); System.out.println("guess my secret number."); // initialize the game Guess game = new Guess(); // Loop until we win or lose the game. We don't know in advance // how many iterations the loop will take, so a while loop seems // appropriate. while (true) { System.out.print("enter your guess: "); int newGuess = Integer.parseInt(kbd.readLine()); if (game.good(newGuess)) { System.out.println("Congratulations! You guessed my number!"); break; } else if (game.isOver()) { System.out.println("Sorry, you have run out of guesses. Better luck " + "next time"); System.out.println("The answer was " + game.giveAnswer()); break; } } } } // How could we modify the program so that if we win, it says how many // guesses it took us? Also, think about how we could modify the class so // that it can give advise in case our guess is really way off, for example // starting off with 90.