Skip to content

Instantly share code, notes, and snippets.

@joeyv
Last active December 28, 2015 01:09
Show Gist options
  • Save joeyv/7417986 to your computer and use it in GitHub Desktop.
Save joeyv/7417986 to your computer and use it in GitHub Desktop.
Guessing game.
import java.util.*;
public class HiLow{
public static void main(String []args){
int num, play, guess, i = 1;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
System.out.println("[1] Play\n[2] Quit");
play = scan.nextInt();
while ( play <= 2 ){
if (play == 1){
System.out.println ("Guess a number 1 = 100");
num = generator.nextInt(100) + 1;
while (play == 1){
i++;
guess = scan.nextInt();
if (guess == num){
System.out.println ("Nice, You got it! \nIt took you " + (i - 1) + " tries to guess the number " + num );
break;
}else if (guess < num){
System.out.println ("Too Low");
System.out.println ("[1] Guess Again [2] Quit");
play = scan.nextInt();
if (play == 2){
System.exit(1);
} else if (play==1){
System.out.println ("Guess Again:");
continue;
}
}else if (guess > num){
System.out.println ("Too High");
System.out.println ("[1] Guess Again [2] Quit");
play = scan.nextInt();
if (play == 2){
System.exit(1);
}else if (play==1){
System.out.println ("Guess Again:");
continue;
}
}
}
}else if (play == 2) {
System.exit(1);
}
System.out.println("[1] Play Again \n[2] Quit");
play = scan.nextInt();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment