Guessing Range
This class helps the user play a simple guessing game by
keeping track of what the user has learned from previous
guesses. It is the solution to an assignment given out in
the fall of 2003. The relevant part of the origianl
assignment follows:
Write a class GuessingRange that does just what GuessingGame (another program on this site) did, but with prompts that includes the range i..j so far, where i is "one more than" the highest guess below the answer, and j is "one less than" the lowest guess above the answer:
Your guess was too high. You know that the number is in the range i..j, inclusive. Guess again.
Your guess was too low. You know that the number is in the range i..j, inclusive. Guess again.
Of course, replace i and j with actual numbers, and think carefully about what exact values they should have.
After 1 guess, you only know i or j. In this case, use an open-ended range, of the form i.. or ..j.
When the user finishes, tell them (using JOptionPane.showMessageDialog): You're right! You used x guesses.
where x is the number of guesses, including the
correct guess.
Download files: GuessingRange.java
Areas: Introduction to loops.
Write a class GuessingRange that does just what GuessingGame (another program on this site) did, but with prompts that includes the range i..j so far, where i is "one more than" the highest guess below the answer, and j is "one less than" the lowest guess above the answer:
Your guess was too high. You know that the number is in the range i..j, inclusive. Guess again.
Your guess was too low. You know that the number is in the range i..j, inclusive. Guess again.
Of course, replace i and j with actual numbers, and think carefully about what exact values they should have.
After 1 guess, you only know i or j. In this case, use an open-ended range, of the form i.. or ..j.
When the user finishes, tell them (using JOptionPane.showMessageDialog):
Download files: GuessingRange.java
Areas: Introduction to loops.
|
|
GuessingRange.java |
|
View full source |
| Question 1 |
| What happens if the user's first guess is too low? |
| Question 2 |
| Why is upper initialized to ""? |
