C# 使用while循环时出错

C# 使用while循环时出错,c#,loops,while-loop,C#,Loops,While Loop,while循环不工作。我得到了一个错误,当它击中打破了循环。错误是没有要中断的循环语句 static void Main(string[] args) { accounts myclass = new accounts(); string userin; myclass.fillAccounts(); //int i = 0; //while (i != 1) do { } while (userin != "x")

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

因为它不是一个循环体

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
}

就代码而言,您已经设法将主体放在实际循环的下面

你有:

do 
{

} while (criteria);
{
   // code you want to execute repeatedly
}
因此,您会得到
break
语句的错误消息,因为它实际上不包含在循环中

您应该只有:

while (criteria)
{
    // code you want to execute repeatedly
} 
省略
do
部分,因为这实际上会在您真正想要循环的代码上方创建一个合法循环

编辑:@Randy,这是你第二次发布类似问题。您正在有效地尝试将
do
while
循环组合起来。去。简而言之,
while
循环检查循环体之前的条件,
do
循环检查循环体之后的条件。
do
循环还使用
while
关键字,这可能会让您感到困惑

Do循环

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.
}
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),然后再创建另一个代码块。

每个人都纠正了你的do/while循环语法,所以我不会强调这一点


要允许用户不区分大小写地输入命令,可以使用
if(userIn.ToUpper()=“X”)
并且不必同时检查这两种情况。

就像您在do中的情况一样,第一个语句是break,因此它从循环中出来,而while中的语句将不会执行…

检查此语句是一个语法错误

字符串myString=“X3”; int i=4; int j=0; 做 { if(myString==“X”+j) { 打破 } j++;
}而(j<4)

请描述您遇到的错误。@randy,编辑问题中的代码并没有真正的帮助(除非您提供了更好的上下文)。您的编辑完全改变了问题的性质,这使得您收到的答案对其他用户无效,这些用户与您最初遇到的问题类似。在VisualStudio中修复代码,保留原始代码,以便将来的用户可以找到解决方案。如果您遇到其他错误,请为这些问题分别发帖。@randy,当您在中声明
userin时,给它一个初始值<代码>字符串userin=null可以正常工作。此新错误与使用未初始化的变量有关(因为您在
部分对其进行比较,但未为其指定原始值)。请不要改变你的问题!发布一个新的!现在我得到了一个新的错误1一个名为“userin”的局部变量已经在这个范围内定义了,因为它太简单了,看起来太难了。我想我会使用while循环while(userin!=“x”,“x”)@randy,据我所知,它的语法无效。执行多重比较时,每次都需要指定比较的两侧。如果您了解集合,您可以创建一个字符串列表并使用
.contains()
,但对于这种情况来说,这太复杂了。