如何制作IF / ELSE语句继续在C#中重启

时间:2017-06-19 12:55:40

标签: c# loops

我有这个代码,我想知道我应该将它包装在什么样的循环中,以便在代码结束后它不会退出控制台程序。

string command = Console.ReadLine();

if(command == ("help"))
    //gives out a list of commands

else if(command == ("quit"))
    //quits the program

else
    //Sorry, I didn't understand you!

2 个答案:

答案 0 :(得分:2)

你正在寻找一个do-while循环。

var validCommands = new string[] { "foo", "bar" };
do
{
    if(command == "foo")
        // do stuff
    else if (command == "bar")
        // do stuff
    else
        // invalid
}
while(!validCommands.Contains(command))

答案 1 :(得分:1)

类似的东西:

do {
  string s = Console.ReadLine();
} while (s != "quit")