Last active
December 28, 2015 01:09
-
-
Save joeyv/7417986 to your computer and use it in GitHub Desktop.
Guessing game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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