Question: C# Magic 8 ball while loop lab For your program, you will need to modify your source code from the previous Magic 8-Ball lab. You

C# Magic 8 ball while loop lab

For your program, you will need to modify your source code from the previous Magic 8-Ball lab. You should now use a loop to let the user ask additional questions. To do this, you will need to enclose the statements responsible for getting the users question, generating a random value, and displaying the response inside the body of a while loop

Additionally, inside of the loop, after giving the user their random answer, ask them a simple yes or no question about whether they would like to continue. Instruct them on what values to answer (Y or N,y or n, Yes or No, etc). The variable that you use to store their response will be your loop control variable (and will need to be initialized before the loop begins)

.As long as the user answers yes, then your loop should repeat, allowing them to ask another question and giving them another answer. When they eventually answer no, your loop should terminate (and your program will end)

Your Program

Your program for this lab simulates a Magic 8-Ball. The Magic 8-Ball is a fortune telling toy in which the player asks any Yes/No question and the Magic 8-Ball randomly answers the question with one of 20possible answers. A description of the game, along with the twenty answers can be found on the Wikipedia page.

For this program, you only need to use the following 5 answers:

- It is certain

-Reply hazy, Try Again

-Dont count on it

-Signs point to yes

-My sources say no

Inside the Main, you should first welcome the user, and prompt them to ask a question. You may choose to use a string variable to store the users question, but unless we want to repeat the question back to them in the output later, we dont actually care what the users question is. We arent actually telling the future just generating a random response.

Instead of storing it in a string, then, we can instead ignore their response. If we use a call to Console. ReadLine(), but do not assign the results to a variable, the user will still be prompted to enter something, but it wont be stored.

After reading the users question, your program will then randomly generate a number between 1 and5. Well use this value to select a random response.

To generate a random number, there are two steps we must take. The first is to declare a variable to represent our random number generator. We can do that using the syntax seen below.

Random rnd = new Random();

With our random number generator declared, we can generate a random number by calling rnd.Next().This function will return a randomly generated value between 0 and the largest value that an int can store (a bit more than 2.1 billion). To restrict this number to something between 1 and 5, we will first calculate the modulus of the value divided by 5 remember that modding by 5 will always return a value between 0 and 4. We can then add 1 to that result to get something from 1 to 5.

Storing the result in a variable, it might look something like

int roll = rnd.Next() % 5 + 1;

Once you have the randomly generated value, you should use an if...else if structure to choose which of the five messages to display, with one belonging to each possible value, 1 5.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!