Skip to content

Instantly share code, notes, and snippets.

Created February 20, 2017 13:40
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 anonymous/72f99fa4fe5e233716c9d8bad3552dc6 to your computer and use it in GitHub Desktop.
Save anonymous/72f99fa4fe5e233716c9d8bad3552dc6 to your computer and use it in GitHub Desktop.
import java.util.Random;
import java.util.Scanner;
public class HiLo
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int tries = 0;
int number = 1 + r.nextInt(100);
String response = "";
int guess;
//FIRST NUMBER GUESS
System.out.print("GUESS MY NUMBER");
System.out.print("\nI'm thinking of a number between 1 and 100. You have 7 guesses");
System.out.print("\nENTER YOUR GUESS. \nGuess #" + tries + "\t");
guess = keyboard.nextInt();
tries++;
if ( guess > number )
System.out.println("TOO HIGH");
else if ( guess < number )
System.out.println("TOO LOW");
//GUESSING NUMBER LOOP
while (guess != number && tries < 7)
{
System.out.print("ENTER YOUR GUESS. \nGuess #" + tries + "\t");
guess = keyboard.nextInt();
if ( guess > number )
System.out.println("TOO HIGH");
else if ( guess < number )
System.out.println("TOO LOW");
tries++;
}
if ( guess == number )
System.out.println("\nRIGHT ON");
else if ( tries == 7 )
System.out.println("\nNO MORE GUESSES");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment