System.ConsoleKey 值用完后如何取消(或分配另一个值)?

How do I nullify (or assign another value to) a System.ConsoleKey value once it's finished being used?

我这里有一个重复的代码,充满了 goto 语句,使这个 while 循环很好......永远重复。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            main();
        }

        public static ConsoleKeyInfo keyPressed;

        private static void main()
        {
            start:
            keyPressed = Console.ReadKey();

            while (true)
            {
                loopstart:

                if (keyPressed.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine("You pressed the Enter Key!");
                    goto loopstart;
                }
                if (keyPressed.Key == ConsoleKey.Escape)
                {
                    Console.WriteLine("You pressed the Escape Key!");
                    goto loopstart;
                }
                if (keyPressed.Key == ConsoleKey.Spacebar)
                {
                    Console.WriteLine("You pressed the Spacebar!");
                    goto loopstart;
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine("You broke the loop!");
            goto start;
        }
    }
}

在不删除任何代码的情况下,是否可以将keyPressed.KeykeyPressed本身的值更改为NULL;声明时的状态,或任何其他 value/key 不是空格键、回车键或转义键的状态?

当然,把代码中的goto loopstart;全部去掉就可以解决问题,但这与问题的重点不符。

我想要做的是使 keyPressed.KeyNULL(或任何其他值),以便所有 IF 语句都将导致错误,因此这意味着不 运行宁goto loopstart代码。


现在的问题是,当我尝试用一​​个简单的 keyPressed = null; 使它无效时,它会出现以下错误:

Cannot convert null to 'System.ConsoleKeyInfo' because it is a non-nullable value type.

有什么方法可以取消(或将值更改为其他值)以便打破循环?
(如:使 IF 语句到达必须 运行 else 代码的地步)

它应该看起来像:

...

{
    loopstart:
    if (keyPressed.Key == ConsoleKey.Enter)
    {
        Console.WriteLine("You pressed the Enter Key!");
        // keyPressed = null; <-- Does not work.
        // Do something to make ConsoleKey.Key to equal something else.
        goto loopstart;
    }
    if (keyPressed.Key == ConsoleKey.Escape)
    {
        Console.WriteLine("You pressed the Escape Key!");

...

显然 // Do something to make ConsoleKey.Key to equal something else. 已替换为工作代码?

如果这有效,第一次循环 运行s(假设开始时按下的键是空格键、Escape 或 Enter 键)将导致使用 goto loopstart;,并且第二轮会跳到 goto start; ,在那里它会要求另一个密钥。
然后该过程以用户输入的速度重复,而不是不停地重复同一个键,或要求另一个键。

基本上:将循环 运行 设为 IF 语句作为适当的 IF 语句而不是 FOR 循环。

See also

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            main();
        }

        public static ConsoleKeyInfo keyPressed;

        private static void main()
        {
        start:
            keyPressed = Console.ReadKey();

            while (true)
            {
            loopstart:

                if (keyPressed.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine("You pressed the Enter Key!");
                    keyPressed = new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false);
                    goto loopstart;
                }
                if (keyPressed.Key == ConsoleKey.Escape)
                {
                    Console.WriteLine("You pressed the Escape Key!");
                    goto loopstart;
                }
                if (keyPressed.Key == ConsoleKey.Spacebar)
                {
                    Console.WriteLine("You pressed the Spacebar!");
                    goto loopstart;
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine("You broke the loop!");
            goto start;
        }
    }
}

为什么要使用goto-statement,这是非常过时的结构。您可以轻松 continue 循环。而 else 检查也是多余的。您可以在检查之前简单地从 Console 中读取密钥,如下所示:

while (true)
{
    keyPressed = Console.ReadKey();

    switch (keyPressed.Key)
    {
        case ConsoleKey.Enter:
            Console.WriteLine("You pressed the Enter Key!");
            continue;

        case ConsoleKey.Escape:
            Console.WriteLine("You pressed the Escape Key!");
            continue;

        case ConsoleKey.Spacebar:
            Console.WriteLine("You pressed the Spacebar!");
            continue;
    }
    // should be outside the switch for breaking the loop
    break;
}

如果要清除keyPressed,使用default构造,像这样:

keyPressed = default(ConsoleKeyInfo);

但是你为什么要这样做呢? Garbage Collection会自己清空内存,不要进去