在java中,退出多重循环可以使用外层循环的控制条件收内层循环的控制来退出。这里使用标号来退出循环。以下是demo:
public class Test
{
public static void main(String[] args)
{
int array[][] = {{ 1, 2, 5, },{ 4, 5, 6 },{ 3, 2, 5 } };
mark: for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length; j++)
{
if (array[i][j] == 5)
{
System.out.println(array[i][j]);
System.out.println("Found number 5! Break from inner loop!");
break mark;
} else
System.out.println(array[i][j]);
System.out.println("I am the outer loop!");
}
}
System.out.println("I am the next code of the mark loop!");
}
}
以下是输出结果:
1
I am the outer loop!
2
I am the outer loop!
5
Found number 5! Break from inner loop!
I am the next code of the mark loop!