Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

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):

image text in transcribedimage text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

2. How much time should be allocated to the focus group?

Answered: 1 week ago

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago