Saturday, 25 March 2017

Random Number Guess Game

import java.util.Random;
import java.util.Scanner;

public class GuessGame{
public static void main(String[] args ){

    Random rand = new Random();
    int numberToGuess=rand.nextInt(5000);
    int numberOfTries = 0;
    Scanner input = new Scanner(System.in);
    int guess;
    boolean win = false;

    while (win==false){
    System.out.println("Guess a number between 1 and 5000: ");
    guess = input.nextInt();
    numberOfTries++;

    if(guess == numberToGuess){
    win = true;
    }
    else if(guess<numberToGuess){
    System.out.println("Your guess is too low");
    }
    else if(guess>numberToGuess){
    System.out.println("Your guess is too high");
    }
}
  
    System.out.println("You Win!");
System.out.println("The number is:" +numberToGuess);
System.out.println("You have tried "+numberOfTries +"times!");
}
}

No comments: