Zebra0.com

alice randomRandom Numbers

Random Numbers

Download this project

Run the addition quiz program and try to understand how it works. Next, change it so that it asks you to multiply instead of add.

public void myFirstMethod() {
  this.bear.say( "Bears are very smart!" );
  this.bear.say( "Let's see how smart you are!" );
  Integer numRight = 0;
  for( Integer indexA = 0; indexA<10; indexA++ ) {
    Integer num1 = RandomUtilities.nextIntegerFromAToBInclusive( 1, 10 );
    Integer num2 = RandomUtilities.nextIntegerFromAToBInclusive( 1, 10 );
    Integer sum = num1+num2;
    Integer answer = this.bear.getIntegerFromUser( "How much is "+num1+"+"+num2 );
    if( answer == sum ) {
      this.bear.say( "Grrreat!" );
      numRight = numRight+1; 
    } else { 
      this.bear.say( "Sorry, the answer is "+sum ); 
    }     
  }
  this.bear.say( "You got "+numRight+" right." );
  this.bear.say( "That is "+10*numRight+"%" ); 
}

Next: Guess the Aliens number