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

How do i continuously check for commands in c#?

我想象的是这样的:

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

一直执行直到键入特定命令,如 "exit":

static void Main(string[] args) {
    var line = System.Console.ReadLine().Trim();

    while(line!="exit") {
        myOperationCommand(line);
        line = System.Console.ReadLine().Trim(); // read input again
    }

    System.Console.WriteLine("End!\n");
}

// Do some operation...
static void myOperationCommand(string line) {
    switch(line) {
        case "checkprice":
            System.Console.WriteLine("price of foo is 42$");
            break;
        default: 
            System.Console.WriteLine("Command not reconized: " + line);
            break;
    }
}

也许这个

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