Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何连续检查c#中的命令?_C#_Command_Prompt - Fatal编程技术网

如何连续检查c#中的命令?

如何连续检查c#中的命令?,c#,command,prompt,C#,Command,Prompt,我想象这样的情况: prompt> checkprice price of foo is 42$ prompt> 运行多个命令。类似于: while(true) { Console.Write("prompt>"); var command = Console.ReadLine(); if (command == "command1") doSomething(); else if (command == "command2") doSome

我想象这样的情况:

prompt> checkprice
price of foo is 42$
prompt>
运行多个命令。

类似于:

while(true) {
    Console.Write("prompt>");
    var command = Console.ReadLine();

    if (command == "command1") doSomething();
    else if (command == "command2") doSomethingElse();
    ...
    else if (command == "quit") break;
}

执行while直到键入特定命令,如“exit”:

也许这个

//this is your price variable
static int price = 42;
//function to check it
static void CheckPrice()
{
    Console.WriteLine("price of foo " + price + "$");
}
static void Main()
{
    bool exit = false;
    do
    {
        //write at begin ">"
        Console.Write(">");
        //wait for user input
        string input = Console.ReadLine();
        // if input is "checkprice"
        if (input == "checkprice") CheckPrice();
        if (input == "exit") exit = true;
    } while (!exit);
}

如果你不知道如何恰当地表达它,想象一下对我和其他人来说理解你的问题是多么困难……如果,为什么还要使用其他的
?你可以在((command=Console.ReadLine().ToLower())!=“quit”){
(这意味着你必须在循环之前声明
command
。---另外,
.ToLower()
用于不区分大小写的命令。@visualincent是的,你可以这样做。我个人不喜欢它,因为1)你必须在循环之外声明命令(因此超出了它的实际范围)2)它在某种程度上不是很可读。1)要点。尽管它没有多大关系。2)我认为这样可读性更高。;——尽管
。ToLower()
仍然可以使用,这样即使您键入
QUIT
QUIT
等,您的程序仍然可以执行您的命令。
//this is your price variable
static int price = 42;
//function to check it
static void CheckPrice()
{
    Console.WriteLine("price of foo " + price + "$");
}
static void Main()
{
    bool exit = false;
    do
    {
        //write at begin ">"
        Console.Write(">");
        //wait for user input
        string input = Console.ReadLine();
        // if input is "checkprice"
        if (input == "checkprice") CheckPrice();
        if (input == "exit") exit = true;
    } while (!exit);
}