C# 控制台应用程序,无法将程序 运行 保留在 while 循环中

C# Console app, Can't keep program running in while loop

我试图在用户按下某些键时更改控制台颜色,但我在 while 循环中做错了。尝试 break 和 return,但两者都不适合我。我还是编码新手。

    static void Main(string[] args)
    {

        while (true )
        {Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");
            Console.ReadLine();

            if (Console.ReadKey(true).Key == ConsoleKey.R)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Clear();
                //Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");
               break;
            }

            else if (Console.ReadKey(true).Key == ConsoleKey.G)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Clear();
                Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");
                //break;
            }

            else if (Console.ReadKey(true).Key == ConsoleKey.B)
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Clear();
               Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");
                //return;
            }

            else if (Console.ReadKey(true).Key == ConsoleKey.D)
            {
                Console.BackgroundColor = ConsoleColor.DarkGreen;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Clear();
                Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");
              // break;
            }

            else if (Console.ReadKey(true).Key == ConsoleKey.C)
            {
                Console.BackgroundColor = ConsoleColor.Cyan;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.Clear();
               Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");
               // break;
            }

            else if (Console.ReadKey(true).Key == ConsoleKey.M)
            {
                Console.BackgroundColor = ConsoleColor.Magenta;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Clear();
                Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");
              //  break;
            }

            else {
                Console.WriteLine("You didn't press Key R,G,B,D,C,M.");
              //Console.ReadLine();
               // return;
            }
         // Console.ReadLine(); 

        }      
    }
}

}

您的代码无法正常工作,因为:您首先希望用户输入一行,然后输入一个键。

第二:如果用户按下 r,while 循环就会中断。

您应该在开始检查用户输入之前阅读按键,因为在每次 if 上您都在期待新的按键。

   while(true)
            {
                Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");

                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.R)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Clear();
                }

                else if (key.Key == ConsoleKey.G)
                {
                    Console.BackgroundColor = ConsoleColor.Green;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Clear();
                }

                else if (key.Key == ConsoleKey.B)
                {
                    Console.BackgroundColor = ConsoleColor.Blue;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Clear();
                }

                else if (key.Key == ConsoleKey.D)
                {
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Clear();
                }

                else if (key.Key == ConsoleKey.C)
                {
                    Console.BackgroundColor = ConsoleColor.Cyan;
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.Clear();
                }

                else if (key.Key == ConsoleKey.M)
                {
                    Console.BackgroundColor = ConsoleColor.Magenta;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Clear();
                }

                else
                {
                    Console.WriteLine("You didn't press Key R,G,B,D,C,M.");
                }

            };

根据你想实现的

change Console color when user press certain keys

Console.ReadLine 每次迭代都是多余的。

在按下 R 键时退出 while 循环也是意外行为。

试试下面的代码。

static void Main(string[] args)
{

  while (true)
  {
    Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color. Press E to Exit");
    switch (Console.ReadKey(true).Key)
    {
      case ConsoleKey.R:
        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Clear();
        break;
      case ConsoleKey.G:
        Console.BackgroundColor = ConsoleColor.Green;
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Clear();
        break;
      case ConsoleKey.B:
        Console.BackgroundColor = ConsoleColor.Blue;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Clear();
        break;
      case ConsoleKey.D:
        Console.BackgroundColor = ConsoleColor.DarkGreen;
        Console.ForegroundColor = ConsoleColor.White;
        Console.Clear();
        break;

      case ConsoleKey.C:
        Console.BackgroundColor = ConsoleColor.Cyan;
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.Clear();
        break;
      case ConsoleKey.M:
        Console.BackgroundColor = ConsoleColor.Magenta;
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Clear();
        break;
      case ConsoleKey.E:
        return;
      default:
        Console.WriteLine("You didn't press Key R,G,B,D,C,M.");
        break;
    }
    Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color");
  }
}