java中,break关键字用于在switch中和循环中,中断程序的执行,如下:
class BreakDemo
{
public static void main(String[] args){
// 跳出单层循环
for(int x = 1;x<10;x++){
for(int y = 1;y<10;y++){
if(y==3){
break;
}
System.out.print(y);
}
System.out.println();//输出换行
}
System.out.println("---------------");
// 带标签跳出多重循环
breakToHere: for (int x = 1;x<10 ;x++ )
{
for(int y=1;y<10;y++){
if(y==3){
break breakToHere;
}
System.out.print(y);
}
System.out.println();//输出换行
}
}
}
类似地,continue 也有略过本次循环(继续剩下的循环),和带标签略过多重本次循环的用法。
转载请注明原文地址: https://ju.6miu.com/read-7474.html