23

我有以下代码:

foreach(// Some condition here)
{
    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                  //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
             }
        }
    }
}

我想要做的是当我if在最后一个循环中点击第二条语句时foreach返回第一个foreach循环。

注意:如果第二条if语句不成立,它应该继续最后一个foreach循环,直到条件不成立。

提前致谢!

4

6 回答 6

34

直接解决此问题的唯一方法是使用goto.

另一个(更好的)选择是重组直到问题消失。例如,通过将内部代码(while + foreach)放入方法中并使用 return 来返回。

于 2013-07-11T10:20:19.623 回答
16

像这样的东西:

resetLoop = false;
for(// Some condition here)
{
    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                  //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
                 resetLoop = true;
                 break;
             }
        }
        if (resetLoop) break;
    }
    if (resetLoop) {
        // Reset for loop to beginning
        // For example i = 0;
    }
}
于 2013-07-11T10:24:38.893 回答
9

没有人提到它(Henk 简要提到过),但最好的方法是将你的循环移动到它自己的方法中并使用return

public ReturnType Loop(args)
{
foreach outerloop
    foreach innerLoop
       if(Condition)
          return;
}
于 2013-07-11T10:27:29.187 回答
7

我看到你接受了这个人提到你的 goto 语句的答案,在现代编程和专家意见中,goto 是一个杀手,我们称它为编程中的杀手,这有一些特定的原因,我不会在这里讨论它此时,但您的问题的解决方案非常简单,您可以在这种情况下使用布尔标志,就像我将在我的示例中演示的那样:

foreach(// Some condition here)
{
    //solution
    bool breakme = false;

    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
            if (// Condition again)
            {
                //Do some code
            }
            if (// Condition again)
            {
                //Stop the first foreach then go back to first foreach
                breakme = true;
                break;
            }
        }
    }
    if(breakme)
    {
        break;
    }
}

简单明了。:)

于 2015-03-19T11:12:16.837 回答
4

正如我之前所说,我还建议重新分解代码,看看是否绝对有必要将 3 个循环嵌套在另一个循环中。如果是,并且我假设涉及一些逻辑,您应该考虑拆分为子函数(如建议的那样)

对于一个简单的代码解决方案:

foreach(// Some condition here)
{
    var broke = false;
    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                  //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
                 broke = true;
                 break;
             }
        }
        if (broke) break;
        // continue your while loop
    }
}
于 2013-07-11T10:26:45.703 回答
3
var broken = false;
foreach(// Some condition here)
{
    broken = false;
    while (!broken && // Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                 //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
                 broken = true;
                 break;
             }
        }
    }
}
于 2013-07-11T10:21:43.217 回答