Allen_L

Number guess

Jul 28th, 2015
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package my_guess;
  2.  
  3. import java.util.Scanner;
  4.  
  5. // Number guessing game . computer picks random number between 0 and max number
  6. // user keeps guessing number and game tells if high or low or correct
  7. // when correct it tells how many guesses it took
  8. // then asks if player wishes to play again
  9. public class Guess {
  10.     private static int rand, guess, count, MAX;
  11.     private static String play_again = null;
  12.     private static Scanner scan;
  13.  
  14.     public static void main(String[] args) {
  15.         scan = new Scanner(System.in);
  16.         MAX = 1000;
  17.         intro_game();
  18.     }
  19.  
  20.     private static void intro_game() {
  21.         System.out
  22.                 .println("This is a number guessing game. Try to guess the number I am thinking of");
  23.         System.out.println("from 1 to 1000 in the least amount of tries.");
  24.         System.out.println("I will tell you if it's higher or lower.");
  25.         get_random_number();
  26.  
  27.     }
  28.  
  29.     private static void get_random_number() {
  30.         rand = (int) ((Math.random() * (MAX - 1) + 1));
  31.         System.out.println("Now guess the number I am thinking of.");
  32.         guess_check();
  33.     }
  34.  
  35.     private static void guess_check() {
  36.         while (true) {
  37.             try {
  38.                 // get input and also check if it's an Integer
  39.                 guess = Integer.parseInt(scan.next());
  40.                 break;
  41.             } catch (NumberFormatException ignore) {
  42.                 System.out.println("It has to be an integer.");
  43.                 count++;// even wrong type of guess is counted
  44.             }
  45.         }
  46.         high_low();
  47.     }
  48.  
  49.     private static void high_low() {
  50.         count++;
  51.         if (guess > rand) {
  52.             System.out.println("To high.");
  53.             guess_check();
  54.         } else if (guess < rand) {
  55.             System.out.println("To low");
  56.             guess_check();
  57.         } else
  58.             System.out.println("Correct, it took you " + count + " tries");
  59.         play_again();
  60.     }
  61.  
  62.     private static void play_again() {
  63.         System.out.println("Would you like to play again? (y or n)");
  64.  
  65.         while (true) {
  66.             try {// keep asking until it's a 'y' or 'n' input regardless of case
  67.                 play_again = scan.next();
  68.                 if (play_again.equalsIgnoreCase("y")) {
  69.                     //reset all
  70.                     rand = 0;
  71.                     guess = 0;
  72.                     count = 0;
  73.                     scan.reset();
  74.                     break;
  75.  
  76.                 } else if (play_again.equalsIgnoreCase("n")) {
  77.                     System.out.println("Thanks for playing.");
  78.                     System.exit(0);
  79.                 }
  80.             } catch (Exception e) {
  81.                 System.out.println("This should never show");
  82.             }
  83.         }
  84.         intro_game(); // restart
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment