// RepeatableGame.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2012-11-02 File created. // 2012-11-04 Last modification. /* This is an improved version of program Game.java. In this program, the 'game' is played inside a while loop, and the user is given the possibility to play several games during a single run of this program. Information from keyboard is read with the nextLine() method in this version. The reason for this is that nextLine() does not work properly after nextInt() is used. nextInt() leaves the line termination character(s) unread which results in that nextLine() receives old data. */ import java.util.* ; class RepeatableGame { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; boolean user_wants_to_play_more = true ; while ( user_wants_to_play_more == true ) { System.out.print( "\n This program is a computer game. Please, type in " + "\n an integer in the range 1 ... 2147483646 : " ) ; int integer_from_keyboard = Integer.parseInt( keyboard.nextLine() ) ; int one_larger_integer = integer_from_keyboard + 1 ; System.out.print( "\n You typed in " + integer_from_keyboard + "." + "\n My number is " + one_larger_integer + "." + "\n Sorry, you lost. I won. The game is over.\n") ; System.out.print( "\n You want to play more (Y/N) ? " ) ; String user_choice = keyboard.nextLine().trim() ; if ( ! user_choice.equalsIgnoreCase( "Y" ) ) { user_wants_to_play_more = false ; } } } }