2009-11-04 59 views
1

我正在開發一個簡單的項目,但我怎麼可以永遠重複一個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"); 
} 
+0

你可以發佈你已經擁有? – SingleNegationElimination 2009-11-04 00:16:36

+0

當然,我可以 – 2009-11-04 00:17:41

+7

注意,'永遠'''很長一段時間'':-) - mjv 0秒前 – mjv 2009-11-04 00:22:13

回答

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

它編譯,但沒問題。 – 2009-11-04 00:23:55

+0

@Slaks。我改變了它 – 2009-11-04 00:25:57

2

你說這個? PS:這是我最喜歡的預處理器黑客之一。但在C#中不起作用,只有C/C++。

#define ever (;;) 

for ever { 
    //do stuff 
} 
2

我不認爲你的問題真的很清楚。但這裏是一個嘗試:)

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

不能單獨使用「如果」語句,因爲當它到達後,你的程序將繼續在你的代碼執行下一條語句。我認爲你所追求的是一個總是評估爲真的'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的傢伙 - 不要恨我)。

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

    //... 
} 
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 
    } 
} 
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"); 
     } 
    } 
+0

這是一個幽默的嘗試?大聲笑 – mmcdole 2009-11-04 00:35:07

+0

我在寫作時笑了起來 – 2009-11-04 00:38:25

+0

弱,但+1無論如何。哈哈 – Dusty 2009-11-04 03:01:31