Console.ReadLine() 尽管处于 while 循环中,但只工作一次

Console.ReadLine() only works once despite it being in a while loop

为了给代码一些上下文,我正在修改游戏“AssaultCube”。

所以这是一个控制台程序。当它启动时,你可以输入一些东西,如果你输入“1”,它会开始循环将健康值设置为 999。但是,您无法输入更多内容,因为循环尚未结束,但为了结束循环,我需要能够输入“1”以将其关闭。我希望每次输入“1”时都能打开和关闭它。这似乎是一个简单的问题,我一直试图让它工作几个小时但没有运气,我的大脑被炸了。提前致谢,如果我的解释不清楚,我很抱歉,我不擅长这些 :D.

while (true)
        {
            string Select;
            Select = Console.ReadLine();

            if (Select == "1") //If the number "1" is typed, do stuff
            {
                int finalHealth = localPLayer + health; //Add the Base and Health addresses together

                if (healthToggle == false)
                {
                    healthToggle = true;
                    Console.WriteLine("\n[1] Unlimited Health activated\n");

                    while (healthToggle) //While Health Toggle is TRUE, do stuff
                    {
                        vam.WriteInt32((IntPtr)finalHealth, 999); //Set finalHealth to 999 in a loop, making you invincible
                        Thread.Sleep(100); //Let CPU rest
                    }
                }
                else
                {
                    healthToggle = false;
                    Console.WriteLine("\n[1] Unlimited Health deactivated\n");

                    vam.WriteInt32((IntPtr)finalHealth, 100); //Set health value back to normal
                }
            }
            Thread.Sleep(100);
        }

我同意 41686d6564,Console.KeyAvailable and Console.ReadKey() 绝对是正确的选择。

试试这个...

static void Main(string[] args)
{
    bool quit = false;
    while (!quit)
    {
        Console.WriteLine("Press Esc to quit, or 1 to start/stop.");
        while (!Console.KeyAvailable)
        {
            System.Threading.Thread.Sleep(100);
        }
        ConsoleKeyInfo cki = Console.ReadKey(true);
        if (cki.Key == ConsoleKey.Escape)
        {
            quit = true;
        }
        else if (cki.Key == ConsoleKey.D1)
        {
            Console.WriteLine("\n[1] Unlimited Health activated\n");
            bool godMode = true;
            while (godMode)
            {
                // ... do something ...
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff") + ": ...something ...");

                System.Threading.Thread.Sleep(100);
                if (Console.KeyAvailable)
                {
                    cki = Console.ReadKey(true);
                    if (cki.Key == ConsoleKey.D1)
                    {
                        godMode = false;
                    }                            
                }
            }
            Console.WriteLine("\n[1] Unlimited Health deactivated\n");
        }
    }
    Console.WriteLine("Goodbye!");
    Console.Write("Press Enter to Quit");
    Console.ReadLine();
}