import java.util.Random; import java.util.Scanner; public class GuessingGameMain { /** * @param args */ public static void main(String[] args) { // Utilities Random generator = new Random(); Scanner reader = new Scanner(System.in); // Player's score int score = 0; System.out.println("Guessing Game!"); // Generate a number; modulo 10 to make it 1 to 10. int randomnumber = Math.abs(generator.nextInt() % 10); // While the player still wants to play.. Iterate while (1 == 1) { System.out.println("Guess a number, 0-100. To quit, type \"quit\" "); // Read the user input String input = reader.nextLine(); // If player wants to quit, then we switch off the playing switch (loop bottoms) if (input.equals("quit")) { break; } // Parse the inputted string as an integer; // throws an exception in case of a non integer value // being entered. We dont catch() this now, but it // would be a worthy improvement. int guess = Integer.parseInt(input); // If they guesses correctly if (randomnumber == guess) { // Give them a point score++; System.out.print("Correct! You gained a point, you now have "); System.out.print(score); System.out.println(" points!"); System.out.println("We're playing again! New number chosen.\n"); // Generate a number; modulo 10 to make it 1 to 10. randomnumber = Math.abs(generator.nextInt() % 10) + 1; } else if (randomnumber > guess) { System.out.println("Too low!\n"); } else if (randomnumber < guess) { System.out.println("Too high\n"); } } System.out.print("Congratulations! You ended the game with "); System.out.print(score); System.out.println(" points."); } }