Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Console.ReadLine()仅工作一次,尽管它处于while循环中_C#_Windows_While Loop_Console_Console Application - Fatal编程技术网

C# Console.ReadLine()仅工作一次,尽管它处于while循环中

C# Console.ReadLine()仅工作一次,尽管它处于while循环中,c#,windows,while-loop,console,console-application,C#,Windows,While Loop,Console,Console Application,为了给代码提供一些上下文,我正在修改游戏“AssaultCube” 这是一个控制台程序。当它启动时,您可以键入内容,如果您键入“1”,它将开始在循环中将健康值设置为999。但是,由于循环尚未结束,您无法键入更多内容,但为了结束循环,我需要能够键入“1”将其关闭。我希望能够在每次输入“1”时打开和关闭此选项。这似乎是一个简单的问题,我一直试图让它工作了好几个小时,但运气不好,我的大脑已经崩溃了。提前谢谢,如果我的解释不清楚,我很抱歉,我不擅长这些:D while (true) {

为了给代码提供一些上下文,我正在修改游戏“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,这绝对是正确的选择

试试这个

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

@41686d6564的副本。。。不太可能,我也会遇到同样的问题。我需要能够做的是,当另一个循环仍在运行时,基本上转到主循环的开始,以激活对游戏的其他修改。。。这有意义吗?我想我需要完全想出别的办法。