Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tenifni
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenifni/2ea0b5239460e25831da to your computer and use it in GitHub Desktop.
Save tenifni/2ea0b5239460e25831da to your computer and use it in GitHub Desktop.
[Game] Java Guessing Game. Randomly creates an integer between 1~1000. User tries to guess the number.
import java.util.Random;
import java.util.Scanner;
public class Practive{
public static void main (String[] args){
Random rand = new Random();
int numberToGuess = rand.nextInt(1000);
int numberOfTries = 0;
Scanner input = new Scanner (System.in);
int guess;
boolean win = false;
while (win == false){
System.out.println ("enter an interger btw 1- 1000");
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");
}
}
if (numberOfTries <= 6){
System.out.println ("You got it! The numeber was "+ numberToGuess + ". It took you "+ numberOfTries +" tries. IMPRESSIVE!!");
}
if (numberOfTries >6){
System.out.println ("Well, you finally got it! The numeber was "+ numberToGuess + ". It took you "+ numberOfTries +" tries. Good Job!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment