永远重复一个If函数

时间:2009-11-04 00:14:27

标签: c# repeat

我正在开发一个简单的项目,但我怎么能永远重复一个If函数(它就像一个命令行)?感谢。

我的代码是这样的:

Console.Write("> ");
var Command = Console.ReadLine();
if (Command == "About") {
    Console.WriteLine("This Operational System was build with Cosmos using C#");
    Console.WriteLine("Emerald OS v0.01");
}

7 个答案:

答案 0 :(得分:9)

你的意思是:

while( !(!(!(( (true != false) && (false != true) ) || ( (true == true) || (false == false) )))) == false   )
   {
       Console.Write("> ");
       if ("About" == Console.ReadLine())
       {
           Console.WriteLine("This Operational System was build with Cosmos using C#");
           Console.WriteLine("Emerald OS v0.01");
       }
   }

答案 1 :(得分:4)

string Command;
while (true) {
  Command = Console.ReadLine();
  if (Command == "About") {
    Console.WriteLine("This Operational System was build with Cosmos using C#");
    Console.WriteLine("Emerald OS v0.01");
  }
}

答案 2 :(得分:3)

你的问题不清楚,但你可能想做这样的事情:

while(true) {    //Loop forever
    string command = Console.ReadLine();
    if (command.Equals("Exit", StringComparison.OrdinalIgnoreCase))
        break;    //Get out of the infinite loop
    else if (command.Equals("About", StringComparison.OrdinalIgnoreCase)) {A
        Console.WriteLine("This Operational System was build with Cosmos using C#");
        Console.WriteLine("Emerald OS v0.01");
    }

    //...
}

答案 3 :(得分:2)

你是说这个?

while(true) {
    if( ...) {
    }
}

PS:这是我最喜欢的预处理器之一。但是在C#中不起作用,只有C / C ++。

#define ever (;;)

for ever {
    //do stuff
}

答案 4 :(得分:2)

我认为你的问题不是很清楚。但这是一次尝试:)

while (true) {
   if (i ==j ) {
     // whatever
   }
}

答案 5 :(得分:1)

您不能单独使用'if'语句,因为当它到达最后时,您的程序将继续执行代码中的下一个语句。我认为你所追求的是一个总是评估为真的'while'陈述。

e.g。

string Command; 
while(true)
{
    Command = Console.ReadLine(); 
    if (Command == "About")
    { 
        Console.WriteLine("This Operational System was build with Cosmos using C#"); 
        Console.WriteLine("Emerald OS v0.01");
    }
} 

这个循环将是不可避免的,除非抛出异常或你执行一个break语句(或者C#中的等价物,我是一个Java人 - 不要讨厌我)。

答案 6 :(得分:1)

我认为你只想要一个简单的while循环(至少)一个退出点。

while(true)
{
    Console.Write("> ");
    var command = Console.ReadLine();
    if (command == "about") {
        Console.WriteLine("This Operational System was build with Cosmos using C#");
        Console.WriteLine("Emerald OS v0.01");
    } else if (command == "exit") {
        break; // Exit loop
    }
}