该continue语句有两种形式,即未标记的和标记的continue语句。第一个示例说明如何使用未标记的continue语句,而第二个示例说明如何使用带标签的continue语句。
package org.nhooo.example.lang;
public class ContinueDemo {
public static void main(String[] args) {
int[] numbers = {5, 11, 3, 9, 12, 15, 4, 7, 6, 17};
int length = numbers.length;
int counter = 0;
for (int number : numbers) {
// 当数字大于或等于10时,跳过
// 当前循环并继续下一个循环,因为
// 我们只想计算小于10的数字。
if (number >= 10) {
continue;
}
counter++;
}
System.out.println("Found " + counter + " numbers less than 10.");
//下面的示例使用了标记的继续语句。在里面
// 在下面的循环中,我们对数组中的数字求和,直到提醒
//该数字除以2等于零。如果提醒是
// 零,我们跳到数组的下一个维度。
int[][] data = {
{8, 2, 1},
{3, 3},
{3, 4, 5},
{5, 4},
{6, 5, 2}};
int total = 0;
outer:
for (int[] aData : data) {
for (int j = 0; j < aData.length; j++) {
if (aData[j] % 2 == 0) {
continue outer;
}
total += aData[j];
}
}
System.out.println("Total = " + total);
}
}