2010-09-15 63 views
0

while循環不起作用。當它遇到休息以脫離循環時,我會收到一個錯誤。錯誤是沒有循環語句打破。使用while循環時出錯

static void Main(string[] args) 
{ 
    accounts myclass = new accounts(); 
    string userin; 


    myclass.fillAccounts(); 
    //int i = 0; 
    //while (i != 1) 
    do 
    { 
    } 
    while (userin != "x") 
    { 
     //use the following menu:    
     Console.WriteLine("*****************************************"); 
     Console.WriteLine("enter an a or A to search account numbers"); 
     Console.WriteLine("enter a b or B to average the accounts"); 
     Console.WriteLine("enter an x or X to exit program"); 
     Console.WriteLine("*****************************************"); 
     Console.Write("Enter option-->"); 
     userin = Console.ReadLine(); 
     if (userin == "a" || userin == "A") 
     { 
      myclass.searchAccounts(); 
     } 
     else if (userin == "b" || userin == "B") 
     { 
      myclass.averageAccounts(); 
     } 
     else if (userin == "x" || userin == "X") 
     { 
      break; 
     } 
     else 
     { 
      Console.WriteLine("You entered an invalid option"); 
     } 
    } 
} 
+0

請描述你得到的錯誤。 – zneak 2010-09-15 03:21:50

+0

@randy,編輯問題中的代碼並不真正有用(除非您提供更好的上下文)。您的編輯完全改變了問題的性質,這使得您收到的答案對其他與您遇到的問題類似問題的用戶無效。在Visual Studio中修復代碼,讓原始代碼保持原樣,以便將來的用戶可以找到解決方案。如果您遇到其他錯誤,請爲這些問題發佈單獨的問題。 – 2010-09-15 03:56:31

+0

@randy,當你聲明'userin'時,給它一個初始值。 'string userin = null;'會工作得很好。這個新的錯誤是關於使用一個你還沒有初始化的變量(因爲你在'while'部分比較它而沒有分配一個原始值)。而且,請停止改變你的問題!發佈一個新的! – 2010-09-15 04:07:40

回答

8

就代碼而言,您已設法將實體放置在實際循環的下方。

您有:

do 
{ 

} while (criteria); 
{ 
    // code you want to execute repeatedly 
} 

結果,你會得到錯誤信息爲您break聲明,因爲它不是,其實包含一個循環內。

您應該只有:

while (criteria) 
{ 
    // code you want to execute repeatedly 
} 

省略到do部分,因爲這實際上創建上面的代碼合法循環你真的想循環。

編輯: @Randy,這是您第二次發佈類似的問題。您正在有效地嘗試組合dowhile循環。去MSDN and review loops。總之,while循環檢查循環體之前的條件,循環體之後檢查循環體dodo循環也使用while關鍵字,這可能會讓您感到困惑。

待辦事項

do 
{ 
    // your code... 
    // this loop is useful when you want the loop to execute at least once, since 
    // the exit condition will be evaluated *after* the first iteration 
} while (condition); 

雖然循環

while (condition) 
{ 
    // your code... 
    // this loop is useful when you want to prevent *any* iterations unless the 
    // original boolean condition is met. 
} 

這是兩個獨立的循環結構。將它們組合起來並不會讓你的循環樂趣翻倍,它只是創建一個單一的循環(do),然後是另一個代碼塊。

6

因爲它不在循環體中。

do 
{ 
    // This is the do-while loop body. 
    // You should put the code to be repeated here. 
    break; // does work 
} 
while(...) 
{ 
    // This is not the do-while loop body. 
    // This is just an ordinary block of code surrounded by the curly braces 
    // and therefore declares a local scope. Variables declared in this 
    // block is not visible outside the braces. 
    break; // doesn't work 
} 
0

每個人都已經在do/while循環的語法中糾正了你,所以我不會強調這一點。

要允許用戶不區分大小寫地輸入他們的命令,您可以使用if(userIn.ToUpper() == "X")而不必檢查兩個條件。

+0

我以爲我會使用while循環,while(userin!=「x」,「X」) – user770022 2010-09-15 03:41:31

+0

@randy這不是我所知的有效語法。執行多重比較時,您需要每次指定比較的兩側。如果你理解集合,你可以創建一個字符串列表並使用'.contains()',但這對於這種場景來說過於複雜。 – 2010-09-15 12:07:22

0

在你的情況做的第一個語句是休息所以它走出循環,並同時聲明將不會被執行...

0

這是一個語法錯誤,你必須檢查這一項。

string myString =「X3」; int i = 4; int j = 0; do if(myString ==「X」+ j) { break; } j ++; } while(j < 4);