Skip to content

Instantly share code, notes, and snippets.

@eginter
Created June 7, 2016 22:33
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 eginter/bd61b06830d4656e8efe52b25920da49 to your computer and use it in GitHub Desktop.
Save eginter/bd61b06830d4656e8efe52b25920da49 to your computer and use it in GitHub Desktop.
Guess a number game
import java.util.Scanner;
public class KeepGuessing {
public static void main( String[] args ) {
Scanner keyboard = new Scanner(System.in);
int secret, guess;
secret = 1 + (int)(Math.random()*10);
System.out.println( "I have chosen a number between 1 and 10." );
System.out.println( "Try to guess it." );
System.out.print( "Your guess: ");
guess = keyboard.nextInt();
while ( secret != guess ) {
System.out.println( "That is incorrect. Guess again." );
System.out.print( "Your guess: ");
guess = keyboard.nextInt();
}
System.out.println( "That's right! You're a good guesser.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment