如何反复运行该程序?

时间:2018-07-27 14:35:29

标签: c#

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

我遇到的问题是它先运行程序,然后运行以下代码行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;
            }

        }
    }
}

2 个答案:

答案 0 :(得分:1)

执行与您的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);
        }
    }
}

答案 1 :(得分:0)

尝试一下

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);
        }
    }
相关问题