Java 如何退出两个嵌套循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6638321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:28:56  来源:igfitidea点击:

How do exit two nested loops?

javaloopsbreaknested-loops

提问by fireshadow52

I have been using Java for quite some time, yet my education in loops is somewhat lacking. I know how to create every loop that exists in java and break out of the loops as well. However, I've recently thought about this:

我已经使用 Java 有一段时间了,但我在循环方面的教育有些欠缺。我知道如何创建 java 中存在的每个循环并打破循环。然而,我最近想到了这个:

Say I have two nested loops. Could I break out of both loops using just one breakstatement?

假设我有两个嵌套循环。我可以只用一个break语句就跳出两个循环吗?

Here is what I have so far.

这是我到目前为止所拥有的。

int points = 0;
int goal = 100;
while (goal <= 100) {
    for (int i = 0; i < goal; i++) {
        if (points > 50) {
           break; // For loop ends, but the while loop does not
        }
        // I know I could put a 'break' statement here and end
        // the while loop, but I want to do it using just
        // one 'break' statement.
        points += i;
    }
}

Is there a way to achieve this?

有没有办法实现这一目标?

采纳答案by sirbrialliance

In Java you can use a label to specify which loop to break/continue:

在 Java 中,您可以使用标签来指定要中断/继续哪个循环:

mainLoop:
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         break mainLoop;
      }
      points += i;
   }
}

回答by Grzegorz Szpetkowski

Yes, you can write break with labele.g.:

是的,您可以使用标签中断,例如:

int points = 0;
int goal = 100;
someLabel:
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         break someLabel;
      }
   points += i;
   }
}
// you are going here after break someLabel;

回答by Dan Tao

There are many ways to skin this cat. Here's one:

有很多方法可以给这只猫剥皮。这是一个:

int points = 0;
int goal = 100;
boolean finished = false;
while (goal <= 100 && !finished) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         finished = true;
         break;
      }
   points += i;
   }
}

Update: Wow, did not know about breaking with labels. That seems like a better solution.

更新:哇,不知道打破标签。这似乎是一个更好的解决方案。

回答by Blessed Geek

Elementary, dear Watson ...

小学,亲爱的华生……

int points = 0;
int goal = 100;

while (goal <= 100) {
  for (int i = 0; i < goal; i++) {
    if (points > 50) {
      goal++;
      break;
    }
  points += i;
  }
}

or

或者

int points = 0;
int goalim = goal = 100;

while (goal <= goalim) {
  for (int i = 0; i < goal; i++) {
    if (points > 50) {
      goal = goalim + 1;
      break;
    }
  points += i;
  }
}

回答by Dipto_Das

You can reset the loop control variables.

您可以重置循环控制变量。

int points = 0;
int goal = 100;
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         i = goal = 101;
      }
   points += i;
   }
}

回答by Kacper Obrzut

You shouldn't use labels in objective language. You need to rewrite the for/while condition.

您不应该使用客观语言中的标签。您需要重写 for/while 条件。

So your code should look like:

所以你的代码应该是这样的:

int points = 0;
int goal = 100;
while (goal <= 100 && points <= 50) {
    for (int i = 0; i < goal && points <= 50; i++) {
        points += i;
    }
}

// Now 'points' is 55