例如,下面的标记循环可改写为while循环:
outer:
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(i*j > 50){
break outer;
}
}
}
改写为:
int i = 0;
while(i<10){
int j=0;
while(j<10){
if(i*j > 50){
break;
}
j++;
}
i++;
}
例如,下面的标记循环可改写为使用异常:
outer:
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(i*j > 50){
throw new BreakOuterLoopException();
}
}
}
改写为:
try{
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(i*j > 50){
throw new BreakOuterLoopException();
}
}
}
}catch(BreakOuterLoopException e){}
注意,需要定义一个自定义异常BreakOuterLoopException。
下一篇:不使用标签打印h1标题