简体   繁体   中英

How to use Do While loop in “Guess my number ” game?

I'm working on this simple task called "Guess my number" and Im trying to implement my code so when user guesses the right number the application displays "Congratulations " and allow the user to choose whether to play again or not. I think a do while loop will do it, but Im still not sure how to implement that. Any ideas?. Thank you in advanced!

Here's my code that runs just fine.

        Random random = new Random();
        while (true)
        {
            int randomNumber = random.Next(1, 5);
            int counter = 1;
            while (true)
            {
                Console.Write("Guess a number between 1 and 5");
                int input = Convert.ToInt32(Console.ReadLine());

                if (input < randomNumber)
                {
                    Console.WriteLine("Too low, try again.");
                    ++counter;
                    continue;
                }
                else if (input > randomNumber)
                {
                    Console.WriteLine("Too high, try again.");
                    ++counter;
                    continue;
                }
                else
                {
                    Console.WriteLine("Congratulations. You guessed the number!");
                    break;
                }
            }
        }

You can add a boolean flag to set inside your outermost loop to break if the user says "No":

Random random = new Random();
        while (true)
        {
            int randomNumber = random.Next(1, 5);
            int counter = 1;
            bool retry = true;
            while (true)
            {
                Console.Write("Guess a number between 1 and 5");
                int input = Convert.ToInt32(Console.ReadLine());

                if (input < randomNumber)
                {
                    Console.WriteLine("Too low, try again.");
                    ++counter;
                    continue;
                }
                else if (input > randomNumber)
                {
                    Console.WriteLine("Too high, try again.");
                    ++counter;
                    continue;
                }
                else
                {
                    Console.WriteLine("Congratulations. You guessed the number!");
                    Console.WriteLine("Would you like to retry? y/n");
                    string answer = Console.ReadLine();
                    if (answer != "y")
                    {
                        retry = false;
                    }
                    break;
                }

            }
            if (!retry) break;

        }

and for the do-while version:

    bool retry = true;
    Random random = new Random();
    do
    {
        int randomNumber = random.Next(1, 5);
        int counter = 1;

        while (true)
        {
            Console.Write("Guess a number between 1 and 5");
            int input = Convert.ToInt32(Console.ReadLine());

            if (input < randomNumber)
            {
                Console.WriteLine("Too low, try again.");
                ++counter;
                continue;
            }
            else if (input > randomNumber)
            {
                Console.WriteLine("Too high, try again.");
                ++counter;
                continue;
            }
            else
            {
                Console.WriteLine("Congratulations. You guessed the number!");
                Console.WriteLine("Would you like to retry? y/n");
                string answer = Console.ReadLine();
                if (answer != "y")
                {
                    retry = false;
                }
                break;
            }

        }
    } while (retry);

Implementing a do.. while loop in your code is easy no much difference

 Random random = new Random();
        do
        {
            int randomNumber = random.Next(1, 5);
            int counter = 1;
            do
            {
                Console.Write("Guess a number between 1 and 5");
                int input = Convert.ToInt32(Console.ReadLine());

                if (input < randomNumber)
                {
                    Console.WriteLine("Too low, try again.");
                    ++counter;
                    continue;
                }
                else if (input > randomNumber)
                {
                    Console.WriteLine("Too high, try again.");
                    ++counter;
                    continue;
                }
                else
                {
                    Console.WriteLine("Congratulations. You guessed the number!");
                    break;
                }
            }while (true);
        }while (true);

Hope that helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM