我怎样才能继续上循环
我有这样的代码:
foreach(int i in Directions)
{
if (IsDowner(i))
{
while (IsDowner(i))
{
continue;
//if (i >= Directions.Count)
//{
// break;
//}
}
//if (i >= Directions.Count)
//{
// break;
//}
if (IsForward(i))
{
continue;
//if (i >= Directions.Count)
//{
// break;
//}
//check = true;
}
//if (i >= Directions.Count)
//{
// break;
//}
if (IsUpper(i))
{
//if (i >= Directions.Count)
//{
// break;
//}
num++;
//check = false;
}
//if (check)
//{
// num++;
//}
}
}
但我想在 while
循环中对 foreach
进行 continue
。我该怎么做?
I have this code:
foreach(int i in Directions)
{
if (IsDowner(i))
{
while (IsDowner(i))
{
continue;
//if (i >= Directions.Count)
//{
// break;
//}
}
//if (i >= Directions.Count)
//{
// break;
//}
if (IsForward(i))
{
continue;
//if (i >= Directions.Count)
//{
// break;
//}
//check = true;
}
//if (i >= Directions.Count)
//{
// break;
//}
if (IsUpper(i))
{
//if (i >= Directions.Count)
//{
// break;
//}
num++;
//check = false;
}
//if (check)
//{
// num++;
//}
}
}
but I want to have continue
for foreach
in while
loop. how can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法从内部循环继续外部循环。
您有两个选择:
不好的一个:在中断内部循环之前设置一个布尔标志,然后检查该标志,如果设置了则继续。
好处:只需将您的大意大利面条代码重构为一组函数,这样您就没有内部循环。
You cannot continue an outer loop from an inner one.
You have two options:
The bad one: set a boolean flag before breaking the inner loop, then check this flag and continue if it is set.
The good one: simply refactor your big spagetti code into a set of functions so you do not have inner loops.
您可以
break
跳出while
循环,并继续执行外部foreach
循环的下一次迭代,这将启动一个新的while
循环:如果在
while
循环之后有一些您不希望在这种情况下执行的其他代码,您可以使用一个布尔变量,该变量将在跳出循环之前设置while
循环使这段代码不执行并自动跳转在forach
循环的下一次迭代中:You could
break
out of thewhile
loop and move on to the next iteration of the outerforeach
loop which will start a newwhile
loop:If you had some other code after the
while
loop that you don't want to be executed in this case you could use a boolean variable which will be set before breaking out of thewhile
loop so that this code doesn't execute and automatically jump on the next iteration of theforach
loop:在我看来,在复杂的嵌套循环中使用 goto 是合理的(是否应该避免使用复杂的嵌套循环是另一个问题)。
你可以这样做:
如果其他人必须处理代码,请小心,确保他们没有 goto-phobic。
In my opinion, using goto is justifiable within complex nested loops (whether or not you should avoid using complex nested loops is a different question).
You could do this:
Just be careful if other people have to deal with the code, make sure they're not goto-phobic.
您可以尝试在内部 for 循环中使用谓词,例如:
希望这对您有帮助。
you could try to use predicates for the inner for loop like:
Hope this helps you.