繁体   English   中英

如何反复运行该程序?

[英]How to run this program repeatedly?

该程序一次尝试即可完美运行,以为我想添加另一个功能,使用户可以选择完全或重复进行游戏。

我遇到的问题是它先运行程序,然后运行以下代码行Console.WriteLine(“ Go again?Y / N”); 在询问用户“输入您的猜测:”后立即运行。 有什么办法可以解决此问题,使程序运行到所需次数就可以结束? 我也尝试了一个更宽的while循环,但没有帮助。

使用系统;

namespace HiLoGame
{
    class Program
    {
        static void Main(string[] args)
        {
            bool correct = false;
            bool repeat = true;
            int guess = 0;
            int totalGuess = 0;

            Random random = new Random();
            int returnNum = random.Next(1, 100);

            Console.WriteLine("*** Welcome to the Hi-Lo game ***");
            Console.WriteLine("The computer chose a number between 1 and 100, you guess it");

            do
            {
                Console.WriteLine("Enter your guess: ");
                guess = int.Parse(Console.ReadLine());

                if (guess < returnNum)
                {
                    Console.WriteLine("Your guess is too low!");
                    totalGuess++;
                }

                else if (guess > returnNum)
                {
                    Console.WriteLine("Your guess is too high!");
                    totalGuess++;
                }

                else if (guess == returnNum)
                {
                    correct = true;
                    totalGuess++;
                    Console.WriteLine("*** YOU GOT IT! ***");
                    Console.WriteLine("Total try: {0}", totalGuess);
                }
            } while (!correct);



            Console.WriteLine("Go again? Y/N");
            string go = Console.ReadLine();
            if (go == "Y" || go == "y")
            {
                repeat = true;
            }
            else
            {
                repeat = false;
            }

        }
    }
}

做与您的gameLoop相同的操作。 简短示例:

do
{
    GameLoop();

    Console.WriteLine("Go again? Y/N");
    go = Console.ReadLine();
} while (go == "Y" || go == "y");

完整示例:

using System;

namespace HiLoGame
{
    class Program
    {
        static void Main(string[] args)
        {
            string go = string.Empty;

            Console.WriteLine("*** Welcome to the Hi-Lo game ***");
            Console.WriteLine("The computer chose a number between 1 and 100, you guess it");
            do
            {
                GameLoop();

                Console.WriteLine("Go again? Y/N");
                go = Console.ReadLine();
            } while (go == "Y" || go == "y");

        }

        private static void GameLoop()
        {
            bool correct = false;
            bool repeat = true;
            int guess = 0;
            int totalGuess = 0;

            Random random = new Random();
            int returnNum = random.Next(1, 100);

            do
            {
                Console.WriteLine("Enter your guess: ");
                guess = int.Parse(Console.ReadLine());

                if (guess < returnNum)
                {
                    Console.WriteLine("Your guess is too low!");
                    totalGuess++;
                }

                else if (guess > returnNum)
                {
                    Console.WriteLine("Your guess is too high!");
                    totalGuess++;
                }

                else if (guess == returnNum)
                {
                    correct = true;
                    totalGuess++;
                    Console.WriteLine("*** YOU GOT IT! ***");
                    Console.WriteLine("Total try: {0}", totalGuess);
                }
            } while (!correct);
        }
    }
}

尝试这个

class Program
    {
        static void Main(string[] args)
        {
            bool repeat = true;
            while (repeat)
            {
                DoTheGame();
                Console.WriteLine("Go again? Y/N");
                string go = Console.ReadLine();
                if (go == "Y" || go == "y")
                {
                    repeat = true;
                }
                else
                {
                    repeat = false;
                }
            }
        }

        public static void DoTheGame()
        {
            bool correct = false;

            int guess = 0;
            int totalGuess = 0;

            Random random = new Random();
            int returnNum = random.Next(1, 100);

            Console.WriteLine("*** Welcome to the Hi-Lo game ***");
            Console.WriteLine("The computer chose a number between 1 and 100, you guess it");

            do
            {
                Console.WriteLine("Enter your guess: ");
                guess = int.Parse(Console.ReadLine());

                if (guess < returnNum)
                {
                    Console.WriteLine("Your guess is too low!");
                    totalGuess++;
                }

                else if (guess > returnNum)
                {
                    Console.WriteLine("Your guess is too high!");
                    totalGuess++;
                }

                else if (guess == returnNum)
                {
                    correct = true;
                    totalGuess++;
                    Console.WriteLine("*** YOU GOT IT! ***");
                    Console.WriteLine("Total try: {0}", totalGuess);
                }
            } while (!correct);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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