C#控制台应用程序,Can';t保持程序在while循环中运行

C#控制台应用程序,Can';t保持程序在while循环中运行,c#,if-statement,while-loop,console-application,C#,If Statement,While Loop,Console Application,当用户按下某些键时,我试图改变控制台的颜色,但我在while循环中做了一些错误的事情。尝试休息和返回,但两者都不适合我。我对编码还是新手 static void Main(string[] args) { while (true ) {Console.WriteLine("Press Key R,G,B,D,C,M to change Console Color"); Console.ReadLine();

当用户按下某些键时,我试图改变控制台的颜色,但我在while循环中做了一些错误的事情。尝试休息和返回,但两者都不适合我。我对编码还是新手

    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.");
                }

            };

根据您想要实现的内容

当用户按某些键时更改控制台颜色

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

预期的行为是什么?定义“不起作用”。它有什么作用?你期望它做什么?你在每一次迭代中都在做一个readline。您确定没有为该读线输入键吗?如果您想在输入键后运行此循环,则必须使用
继续
,而不是
返回
中断
,因为这些关键字将离开循环<代码>继续,另一方面,将开始新的迭代。+1到kostbone。。。这显然是一个家庭作业问题,他给出了足够的答案,让Vishal跟上进度,让他解决剩下的问题。来自@Zippy works的代码,我试图通过按下键并保持循环来更改控制台颜色,所以它不会退出应用程序。谢谢大家。谢谢@tchelidze你们的代码也得到了我想要的东西。@VishalBarot很高兴能得到帮助。