C#Console app,无法在while循环中保持程序运行

时间:2016-01-25 13:40:16

标签: 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();

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

        }      
    }
}

}

2 个答案:

答案 0 :(得分:2)

根据您要实施的内容

  

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

每次迭代的

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

答案 1 :(得分:1)

您的代码无效,原因是:您首先要求用户输入一行,然后输入一个密钥。

第二:如果用户按下了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.");
                }

            };