Question
In this assignment you are going to program a number guessing game in which you have 10 chances to guess a number between 1 and
In this assignment you are going to program a number guessing game in which you have 10 chances to guess a number between 1 and 100.
A sample run is show below:
In the program you will need a random number between 1 and 100. Review the section on generating random numbers in your text.
Just because a block of code has a definite number of iterations doesnt mean that you need to use a for loop. The fact that you have ten guesses may lead you to think that you need ten iterations. But you might guess the number your first try. So, you cant tell how many iterations you are going to need when you start the game. Therefore, this program can be more naturally written with a while or do - while loop.
In this program you are going to use a Boolean variable to control the main loop. You should review the section in your text that discusses using Boolean variables as sentinel values.
Your program should only accept integers between 1 and 100. Review the section on do-loops in your text for an example of how to do this. If the user tries to guess a number outside of this range, you should print an error message and reprompt the user for a guess. Notice that an out-of-range guess does not count against the number of guesses.
In order to practice different types of loops, we are imposing a structural constraint on this problem. This program requires a nested loop structure. The inner loop is a do-while loop that checks to make sure the user enters a valid value. The outer (main) loop is a while (!done) loop that controls the game using the Boolean variable done. Programs that do not use this structure will not meet specifications and will have to be redone.
In other words, your main method should have the following structure (pseudocode):
run You have 10 tries to guess a number between 1 and 100 Guess number 1: 50 Your guess is too high. Try again Guess number 2: 25 Your guess is too high. Try again Guess number 3: 15 Your guess is too high. Try again. Guess number 4 5 Your guess is too low. Try again Guess number 5: 10 Your guess is too low. Try again Guess number 6: 12 Your guess is too low. Try again Guess number 7: 11 Your guess is too low. Try again Guess number 8: 13 Your guess is too lo. Try again. Guess number 9 14 Congratulations! You have correctly guessed the number in 9 tries BUILD SUCCESSFUL (total time: 35 seconds)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started