控制台应用程序中的用户输入命令 [英] User input commands in Console application

查看:139
本文介绍了控制台应用程序中的用户输入命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望控制台应用程序具有用户类型 / help 之类的命令,并且控制台可以编写帮助。我希望它使用 switch 如:

I would like my console application to have commands like user types /help and console writes help. I would like it to use switch like:

switch (command)
{
    case "/help":
        Console.WriteLine("This should be help.");
        break;

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

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

如何实现?

推荐答案

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

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

这篇关于控制台应用程序中的用户输入命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆