如何使用Do While循环"猜猜我的号码"游戏?

时间:2016-02-03 05:34:30

标签: java c#

我正在处理这个简单的任务,叫做#34;猜猜我的号码"我试图实现我的代码,所以当用户猜出正确的数字时,应用程序显示"祝贺"并允许用户选择是否再次播放。我认为while 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;
                }
            }
        }

2 个答案:

答案 0 :(得分:3)

如果用户说'#34;不&#34;:

,您可以在最外层循环内添加一个布尔标志来设置。
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;

        }

和do-while版本:

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

答案 1 :(得分:1)

在你的代码中实现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);

希望有帮助...