且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

控制台应用程序中的用户输入命令

更新时间:2023-02-19 19:39:04

根据您对勘误表的答案,您似乎希望一直循环播放直到被告知不要循环播放,而不是在启动时从命令行获取输入。在这种情况下,您需要在开关之外循环以保持运行。这是一个基于您上面所写内容的快速示例:

Based on your comment to errata's answer, it appears you want to keep looping until you're told not to do so, instead of getting input from the command line at startup. If that's the case, you need to loop outside the switch to keep things running. Here's a quick sample based on what you wrote above:

namespace ConsoleApplicationCSharp1
{
  class Program
  {
    static void Main(string[] args)
    {
        string command;
        bool quitNow = false;
        while(!quitNow)
        {
           command = Console.ReadLine();
           switch (command)
           {
              case "/help":
                Console.WriteLine("This should be help.");
                 break;

               case "/version":
                 Console.WriteLine("This should be version.");
                 break;

                case "/quit":
                  quitNow = true;
                  break;

                default:
                  Console.WriteLine("Unknown Command " + command);
                  break;
           }
        }
     }
  }
}