Random Numbers

Download the alien number game.

public void myFirstMethod() {
    Integer myNumber = RandomUtilities.nextIntegerFromAToBInclusive( 1, 100 );
    this.alien.say( "I'm thinking of a number from 1 to 100.", Say.duration( 10.0 ) );
    Integer guess = this.alien.getIntegerFromUser( "What is your first guess?" );
    while ( guess! = myNumber ) {
        if( guess<myNumber ) {
            this.alien.say( "My number is more than "+guess, Say.duration( 2.0 ) );
        } else {
            this.alien.say( "My number is less than "+guess, Say.duration( 2.0 ) );
        }
        guess = this.alien.getIntegerFromUser( "What is your next guess?" );
    }
    this.alien.say( "You finally guessed it!" );
} 

Run the program, then modify as below.

  • Count how many guesses the user takes to guess the number.
  • Keep track of what the user should know is the min and max.
  • Tell the user if he makes a mistake: i.e. If he guesses 50 and you say its higher, then later he guesses 40, that is a mistake.
  • Write a new game where the user thinks of a number and the alien tries to guess it.

NEXT: A Penguin Jumps Up and Down