2010-09-15 62 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);