2

我正在开发一个简单的项目,但是我怎样才能永远重复一个 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");
}
4

7 回答 7

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");
       }
   }
于 2009-11-04T00:31:13.047 回答
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");
  }
}
于 2009-11-04T00:22:21.700 回答
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");
    }

    //...
}
于 2009-11-04T00:22:54.943 回答
2

你是这个意思?

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

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

#define ever (;;)

for ever {
    //do stuff
}
于 2009-11-04T00:18:34.627 回答
2

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

while (true) {
   if (i ==j ) {
     // whatever
   }
}
于 2009-11-04T00:18:51.163 回答
1

您不能单独使用“if”语句,因为当它结束时,您的程序将继续执行代码中的下一条语句。我认为您所追求的是始终评估为真的“while”语句。

例如

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 人 - 不要恨我),否则这个循环将是不可避免的。

于 2009-11-04T00:20:36.213 回答
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
    }
}
于 2009-11-04T00:23:02.357 回答