2

I'm working on a C# assignment and am pretty much done, except I cannot figure out how to replay the loop based on the user answering "yes" or "no." If they answer yes I want the loop to replay, if they answer no, it will leave a nice goodbye message.

Here's my code (deleted my commenting for simplicity):

Random randomNumber = new Random();

        int count = 0;
        int actualNumber = randomNumber.Next(1, 50);
        int userGuess = 0;
        bool correct = false;


        Console.WriteLine("In this program you will be prompted to guess a number between 1 and 50 \n\n" +
            "I'll help by saying if your guess is higher/lower than the actual number\n\n\n\n" +
            "I'm thinking of a number between 1 and 50, please enter your guess.\n\n");


        while (!correct)
            {
                count++;

                Console.Write("Guess: ");
                string input = Console.ReadLine();

                if (!int.TryParse(input, out userGuess))
                {
                    Console.WriteLine("That's not a number between 1 and 50, please try again.");
                    continue;
                }

                if (userGuess < actualNumber)
                {
                    Console.WriteLine("Sorry, the number is higher than that, keep guessing!");
                }
                else if (userGuess > actualNumber)
                {
                    Console.WriteLine("Sorry, the number is lower than that, keep guessing!.");
                }
                else
                {
                    correct = true;
                    Console.WriteLine("{0} is the right number! It took you         {1} times to guess", userGuess, count);
                }
            }

I'm having trouble figuring out if I should use another while statement or an if/else or what and where to use it. I've searched here and found similar questions for different languages, but nothing for C#. I would appreciate any help. Thanks!

0

1 Answer 1

4

You can place your code inside do-while loop. And it will ask from user if he or she wants to replay? And if user enters Yes, then the game will start again.

Random randomNumber = new Random();

do
{  
    int actualNumber = randomNumber.Next(1, 50);
    int count = 0;
    int userGuess = 0;
    bool correct = false;

    Console.Clear();
    Console.WriteLine("In this program you will be prompted to guess a number between 1 and 50 \n\n" +
"I'll help by saying if your guess is higher/lower than the actual number\n\n\n\n" +
"I'm thinking of a number between 1 and 50, please enter your guess.\n\n");

    while (!correct)
    {
         // ...your code
    }

    Console.WriteLine("Do you want to replay?");
} while (Console.ReadLine().ToUpper() == "YES");

P.S: Initilize Random outside the loop as example in above, otherwise it will generate same number.

Sign up to request clarification or add additional context in comments.

17 Comments

Thank you for this answer! It works great, but now I'm not running into another issue, I thought this would be a simple thing. I want to clear the screen if the user chooses to play again and NOT show the opening message again, I just want it to prompt "Guess: " I've placed the Console.Clear() command everywhere I can think to put it and it either clears the screen in the wrong spot or it clears it altogether. Can you clarify where I should place my Console.Clear() command? Thanks again!
@bnr32jason It will not clear part of console automatically. But, yes. You can use Console.Clear() inside loop and don't forget to put Console.WriteLine() after this. I have updated my answer.
Thanks for the attempt, but with the Console.Clear() in that position it leaves the opening welcome message "In this program you wil......". Basically I want this welcome message to appear the first time the user runs the program, but immediately after they type "yes" to try again, it clears the screen and only leaves the "Guess:" prompt. I'm really struggling to figure out where to put it. I've went through the debugging process to follow the steps and find where to place it, but nothing gets me the result I want. Thanks again for all your help!
@bnr32jason Have you tried my update? It will always add welcome message to the same place.
I apologize for not explaining myself clearly enough. I only want the welcome message to appear one time. When the user tries again, I don't want the welcome message displayed, only the "Guess:" prompt. So immediately after they type "yes" they have a mostly blank screen in front of them with nothing but the "Guess:" prompt. Hope that makes sense, thanks for the help and sorry for not being clear enough.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.