Colorado State University

Lab 9 - Loops again!

CS160: Foundations in Programming


The purpose of this exercise is
Your GTA will lead you through the following:

Objective: To guess the number our Java program has generated

Write a java program to generate a random number between 0 and 100. Call your program numberGuesser.java.

Use loops to repeatedly guess a number and give feedback to the user, such as, "The number you guessed is too large" or "The number you guessed is too small", so that in repeated guesses the user can use that information to increase his/her chances of getting it right.

If the user does not type in a number, then criticize the user, and read again.

Also, limit the user to a maximum of ten guesses.

Sample output 1 (successful case):

    I am thinking of a number between 0 and 100.
    What is your guess? 50
    The number is too small
    What is your guess? fish
    "fish" is not a number!
    What is your guess? 75
    The number is too small
    What is your guess? 87
    The number is too large
    What is your guess? 80
    You have got it in 5 guesses!

Sample output 2 (unsuccessful case):

    I am thinking of a number between 0 and 100.
    What is your guess? 10
    The number is too small
    What is your guess? 11
    The number is too small
    What is your guess? 12
    The number is too small
    What is your guess? 14
    The number is too small
    What is your guess? 34
    The number is too small
    What is your guess? 23
    The number is too small
    What is your guess? 45
    The number is too small
    What is your guess? 78
    The number is too large
    What is your guess? 31
    The number is too small
    What is your guess? 22
    The number is too small
    Better luck next time!

Early finishers: Extended version of the numberGuesser

Extend your program so that the user can play repeatedly until he/she types in Q to quit. Following which, print out the game stats like number of games played, number of successful attempts, number of failed attempts, number of times the user guessed between 1 to 3, 4 to 6 and 7 to 9 guess ranges.
You must show your GTA your work to get credit for this lab completion.
© 2009 CSU