我正在开发一个简单的项目,但我怎么可以做一个永远重复一个如果功能(这就像一个命令行)?感谢。

我的代码是这样的:

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");
}
有帮助吗?

解决方案

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

其他提示

以任何机会你的意思是:

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

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

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

    //...
}

我不认为你的问题非常清晰。但这里是企图:)

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

您说这个?

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

PS:这是我最喜欢的预处理程序的黑客之一。在C#不工作,虽然,只有C / C ++。

#define ever (;;)

for ever {
    //do stuff
}

我觉得你只是想(至少)一个出口点的简单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
    }
}

您不能自行使用“如果”语句,因为当它到达后,你的程序将继续在你的代码执行下一条语句。我想你追求的是一个“而”语句始终计算为真。

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的家伙 - 不要恨我)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top