【问题标题】:Error when using while loop使用while循环时出错
【发布时间】:2010-09-15 03:19:44
【问题描述】:

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

【问题讨论】:

  • 请描述您遇到的错误。
  • @randy,编辑问题中的代码并没有真正的帮助(除非您提供更好的上下文)。您的编辑完全改变了问题的性质,这使得您收到的答案对于与您最初遇到的问题相似的其他用户无效。在 Visual Studio 中修复您的代码,让您的原始代码保持不变,以便将来的用户可以找到解决方案。如果您遇到其他错误,请针对这些问题发布单独的问题。
  • @randy,当你声明userin时,给它一个初始值。 string userin = null; 可以正常工作。这个新错误与使用尚未初始化的变量有关(因为您在 while 部分比较它而没有为其分配原始值)。请停止改变你的问题!发布一个新的!
  • 现在我收到一个新错误 错误 1 ​​一个名为 'userin' 的局部变量已在此范围内定义
  • 对于如此简单的事情似乎如此愚蠢地困难

标签: c# loops while-loop


【解决方案1】:

就代码而言,您已设法将主体置于实际循环之下。

你有:

do 
{

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

因此,您会收到 break 语句的错误消息,因为它实际上并未包含在循环中。

你应该只有:

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

省略do 部分,因为这实际上会在您真正想要循环的代码之上创建一个合法循环。

编辑: @Randy,这是您第二次发布类似问题。您正在有效地尝试组合 do 和 while 循环。转到MSDN and review loops。简而言之,while 循环检查循环体之前的条件,do 循环检查循环体之后的条件。 do 循环也使用了 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循环

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

这是两个独立的循环结构。将它们组合起来不会使您的循环乐趣加倍,它只是创建一个循环(do),然后是另一个代码块。

【讨论】:

    【解决方案2】:

    因为它不在循环体中。

    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
    }
    

    【讨论】:

      【解决方案3】:

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

      要允许用户输入命令时不区分大小写,您可以使用if(userIn.ToUpper() == "X"),而不必同时检查这两个条件。

      【讨论】:

      • 我想我会使用 while 循环 while(userin!="x","X")
      • @randy 据我所知,这不是有效的语法。进行多重比较时,每次都需要指定比较的两边。如果您了解集合,则可以创建一个字符串列表并使用.contains(),但这对于这种情况来说过于复杂。
      【解决方案4】:

      在你的情况下,第一个语句是 break,所以它会退出循环并且不会执行 while 中的语句......

      【讨论】:

        【解决方案5】:

        您检查了这个是语法错误。

        字符串 myString = "X3"; 诠释 i = 4; 整数 j = 0; 做 { if (myString == "X" + j) { 休息; } j++; } 而 (j

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-06
          • 1970-01-01
          • 2016-02-14
          • 1970-01-01
          相关资源
          最近更新 更多