Advertisement
Guest User

my java text based number guessing game

a guest
Apr 14th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class game
  5. {
  6.     public static void main(String Args [])
  7.     {
  8.        System.out.println("Let's play Guess that Game!");
  9.        
  10.        Random rand = new Random();
  11.        Scanner getInput = new Scanner(System.in);
  12.        Boolean areWeDone = false;
  13.        int high = 100;
  14.        int low = 1;
  15.        int guess = 0;
  16.        int computerNumber = 0;
  17.        int tries = 0;
  18.        
  19.        computerNumber = rand.nextInt(100)+1;
  20.        
  21.        while (areWeDone == false)
  22.        {
  23.            ++tries;
  24.            System.out.println("Guess a number between " +low+" and "+high+".");
  25.            guess = getInput.nextInt();
  26.            
  27.            if (guess < high && guess > low)
  28.            {
  29.                if (guess == computerNumber)
  30.                {
  31.                    System.out.println("You did it! You have guessed the correct answer.");
  32.                    System.out.println("You  took "+tries+ " tries.");
  33.                    areWeDone = true;
  34.                }
  35.                
  36.                else if (guess > computerNumber)
  37.                {
  38.                    System.out.println("You have answered incorrectly. Please try again.");
  39.                    high = guess;
  40.                }
  41.                
  42.                else if (guess < computerNumber)
  43.                {
  44.                    System.out.println("You have answered incorrectly. Please try again.");
  45.                    low = guess;
  46.                }
  47.            }
  48.            
  49.            else
  50.            {
  51.                System.out.println("You have an out of range guess. Stop being stupid and read the question.");
  52.            }
  53.        }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement