leonteale

guessing game

Feb 17th, 2012
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class guessinggame {
  5.      public static void main(String[] args) {
  6.  
  7.         Random rand = new Random();
  8.         int numberToGuess = rand.nextInt(1000);
  9.         int numberOfTries = 0;
  10.         Scanner input = new Scanner(System.in);
  11.         int guess;
  12.         boolean win = false;
  13.  
  14.         while (win == false) {
  15.  
  16.         System.out.println("Guess a number between 1 and 1000: ");
  17.         guess = input.nextInt();
  18.         numberOfTries++;
  19.  
  20.         if (guess == numberToGuess) {
  21.                 win = true;
  22.         }
  23.         else if (guess < numberToGuess) {
  24.                 System.out.println("Your guess is too low");
  25.                 }
  26.         else if (guess > numberToGuess) {
  27.                 System.out.println("Your guess is too high");
  28.                 }
  29.      }
  30.                 System.out.println("You Win!");
  31.                 System.out.println("The number was " + numberToGuess);
  32.                 System.out.println("It took you " + numberOfTries + " tries");
  33.    }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment