java中的break是指跳出標簽指定的語句后繼續執行,如以下的程式,break three的意識是結束three之後的語句塊后繼續執行該語句塊之後的語句,在本程序中就是System.out.println("after block three.");class break1 {
public static void main(String args[]) {
int i;
for(i=1; i<4; i++) {
  one: {
two:  {
three: {         System.out.println("\n i is " + i);
        if(i==1) break one;
        if(i==2) break two;
        if(i==3) break three;
        }//end of three
                                System.out.println("after block three.");
                         
                         }//end of two
                         System.out.println("after block two.");
                   }//end of one
                  System.out.println("after block one.");
                }//end of for
               System.out.println("after for.");
         }
 } 
class break2{
public static void main(String [] args){
ouuter:
for(int i=0;i<4;i++){
System.out.println("in for");
break ouuter;
}
System.out.println("out of for");


for1:
for(int i=0;i<4;i++){
for2:
for (int j=0;j<4;j++){
System.out.println("i:"+i+"  j:"+j);
if (j==2)
   break for1;
}//end for2
}//end for1
System.out.println("end of for1");
}
}

解决方案 »

  1.   

    简单给你分析一下:
    one: {//标示为第一层;
    two:  {//标示为第二层;
    three: {//标示为第三层;        System.out.println("\n i is " + i);
            if(i==1) break one;//跳出第一次层;执行one:{}后面的语句!
            if(i==2) break two;//跳出第二次层;执行two:{}后面的语句!
            if(i==3) break three ;//跳出第三次层;执行three:{}后面的语句!
          }
            }
        }
      

  2.   

    晕丫!楼上两位仁兄解释得很详细,偶非常感谢!可是偶还是不明白◎#¥%※×执行到 break two; 时,执行two:{}后面的语句,可是后面只有 System.out.println("after block two.");可执行呀。而 System.out.println("after block one.");这一句 是在 one:{} 后面的语句,怎么还要加上???偶是初学,希望各位大侠解释得通俗易懂些,谢谢!
      

  3.   

    执行完System.out.println("after block two.");后是one的“}”,然后是System.out.println("after block one.");,然后是for的“}”,接着返回到循环for(){开始处,此时i=3.
      

  4.   

    当执行到 break two以后,它只不过是跳出了two:{}(这就像嵌套循环用break跳出内循环一样),程序还是要按顺序往下走的,接着便执行System.out.println("after block two."),下来就是System.out.println("after block one.")......(我也是初学的,有什么不对,望大家指点^_^)。
      

  5.   

    class break {6
    public static void main(String args[]) {5int i;//define i
    for(i=1; i<4; i++) {4 执行for循环!
    one: {1
    two:  {2
    three: {3//标签1,2,3        System.out.println("\n i is " + i);//打印i=1,2,3
            if(i==1) break one;//执行1'后的语句
            if(i==2) break two;//执行2‘后的语句
            if(i==3) break three;//执行3'后的语句
          }3'
          System.out.println("after block three.");
         }2'
         System.out.println("after block two.");
        }1'
        System.out.println("after block one.");
       }4'
       System.out.println("after for.");
      }5'
     } 6'
    这样应该清楚了吧!好好看看基础吧!!