Guessing numbers
No. 79
Q: |
Write a simple game asking a user to guess a randomly chosen number. Your program will select an integer (pseudo) random number between zero and an exclusive configurable fixed upper limit e.g. 10. The user will have a configurable number of tries to guess the number in question. Each time after entering a number your program will respond with either of the following:
The system offers a maximum number of tries e.g. 6. In the following example the system initially selected the secret number 5. A possible dialogue looks like: Try to guess my secret number between 0 and 10: You have 5 attempts Input your guess:4 Value is too low Input your guess:6 Value is too high Input your guess:5 Congratulations, you won! Regarding reading user input and creating random numbers please use the following recipe to get started:
|
A: |
In case you don't like ... boolean numberWasFound = false; for (int i = 0; !numberWasFound && i < numOfUserAttempts; i++) { ... } else { numberWasFound = true; // No »break« statement required anymore } } ... |