while loop Case Study


Problem: Write a program that plays a reverse guessing game with the user. The user thinks of a number between 1 and 10, and the computer repeatedly tries to guess it.

Sample Run:
Think of a number between 1 and 10. Then I, the computer, will try to guess it.

Is it 6? (high/low/right) low
Is it 8? (high/low/right) low
Is it 9? (high/low/right) right

I guessed your number in 3 guesses!


Sentinel Loop: We will need a loop that ends when the user inputs sentinel value.
What is the sentinel value?




Decomposing the Problem - One Way




Psuedocode:
high-level
Initialization: initialize program's variables
Process computer's "guesses"/play game
Finish up: calculate and display results


More Detail:
main method:
    Create Scanner object
    Call method that plays the game
    Call method that displays results

play method:
    initialize counter to track number of guesses made by computer
    Generate the computer's first guess
    while the computer hasn't guessed correctly:
          display feedback on guess
          update counter
          generate next guess
    return counter

displayResults method:
    takes the number of guesses made as an argument, and prints results