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

ayt*_*thy 1 .net c# console input console-application

我这里有一个重复的代码,充满了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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

当然,可以通过删除goto loopstart;代码中的所有内容来解决问题,但这违背了问题的要点。

我想要做的是设置该keyPressed.KeyNULL(或任何其他值),以便所有IF语句都将导致 false,这意味着不运行代码goto loopstart


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

无法将 null 转换为“System.ConsoleKeyInfo”,因为它是不可为 null 的值类型。

有什么方法可以使我无效(或将值更改为其他值)以便我可以打破循环?
(如:使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!");

...
Run Code Online (Sandbox Code Playgroud)

显然是用// Do something to make ConsoleKey.Key to equal something else.工作代码替换的?

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

基本上:使循环将IF语句作为正确的IF语句而不是FOR循环来运行。

也可以看看

VMA*_*Atm 5

为什么使用goto- 语句,它是非常过时的结构。您可以轻松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;
}
Run Code Online (Sandbox Code Playgroud)

如果你想清除keyPressed,请使用default构造,如下所示:

keyPressed = default(ConsoleKeyInfo);
Run Code Online (Sandbox Code Playgroud)

但为什么要这样做呢?垃圾收集会自行清除内存,您不应该进入那里。