编译运行下列代码时会发生什么情况?
public class LabLoop{
public static void main(String argv[]){
LabLoop ml = new LabLoop();
ml.amethod();
mainmethod:
System.out.println("Continuing");
}
public void amethod(){
outer:
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
40
if(j>1)
break mainmethod;
System.out.println("i "+ i + " j "+j);
}
}//End of outer for
}
}
答案 3) Compile time error
你不能武断的跳入另一个方法,在goto 语句中会带来很多有害的结果。
有俩个问题
1。break mainmethod;
2。outer:
1和2在这里都是什么语法阿,有这么用的么??
public class LabLoop{
public static void main(String argv[]){
LabLoop ml = new LabLoop();
ml.amethod();
mainmethod:
System.out.println("Continuing");
}
public void amethod(){
outer:
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
40
if(j>1)
break mainmethod;
System.out.println("i "+ i + " j "+j);
}
}//End of outer for
}
}
答案 3) Compile time error
你不能武断的跳入另一个方法,在goto 语句中会带来很多有害的结果。
有俩个问题
1。break mainmethod;
2。outer:
1和2在这里都是什么语法阿,有这么用的么??
mainmethod和outer是一段代码块的标记,可以使用goto跳进去,break mainmethod就是跳出代码块;
不过你这段程序有点奇怪,break mainmethod不在mainmethod的代码块内部,编译都不会通过吧。。
把break mainmethod改成break outer应该就可以了