如何在“猜我的号码”游戏中使用Do While loop

本文关键字:游戏 Do loop While 猜我的号码 号码 我的 | 更新日期: 2023-09-27 18:32:55

我正在处理这个名为"猜我的号码"的简单任务,我试图实现我的代码,所以当用户猜到正确的数字时,应用程序会显示"恭喜",并允许用户选择是否再次播放。我认为 do while 循环会做到这一点,但我仍然不确定如何实现它。有什么想法吗?提前谢谢你!

这是我运行良好的代码。

        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;
                }
            }
        }

如何在“猜我的号码”游戏中使用Do While loop

您可以添加一个布尔标志,以便在最外层循环中设置,以便在用户说"否"时中断:

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;
        }

对于做同时版本:

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

在代码中实现 do.. while 循环很容易,没有太大区别

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

希望有帮助...