真善美

          勇于承擔責任; 如果你做不到,要提前跟別人說明,不要等到事情發生后再解釋。

            BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理 ::
            55 隨筆 :: 0 文章 :: 29 評論 :: 0 Trackbacks
          整理一下今天下午同事出的一道題:
          1)
           1 public class Switch {
           2     public static void main(String[] args){
           3         int x=0;
           4         switch(x){
           5         default:
           6             System.out.println("default");
           7         case 1:
           8             System.out.println(1);
           9         case 2:
          10             System.out.println(2);
          11         }
          12     }
          13 }
          輸出結果如下:
          default
          1
          2

          2)
           1 public class Switch {
           2     public static void main(String[] args) {
           3         int x = 0;
           4         switch (x) {
           5         default:
           6             System.out.println("default");
           7         case 0:
           8             System.out.println(0);
           9         case 1:
          10             System.out.println(1);
          11         case 2:
          12             System.out.println(2);
          13         }
          14     }
          15 }

          輸出結果如下:
          0
          1
          2

          3)

           1 public class Switch {
           2     public static void main(String[] args) {
           3         int x = 0;
           4         switch (x) {
           5         case 0:
           6             System.out.println(0);
           7         case 1:
           8             System.out.println(1);
           9         case 2:
          10             System.out.println(2);
          11         default:
          12             System.out.println("default");
          13         }
          14     }
          15 }

          輸出結果如下:
          0
          1
          2
          default

          總結:
          switch表達式的值決定選擇哪個case分支,如果找不到相應的分支,就直接從"default" 開始輸出。
          當程序執行一條case語句后,因為例子中的case分支中沒有break 和return語句,所以程序會執行緊接于其后的語句。

          posted on 2007-06-26 17:11 真善美 閱讀(30770) 評論(19)  編輯  收藏 所屬分類: Java學習
          主站蜘蛛池模板: 海晏县| 邳州市| 常宁市| 闽清县| 墨玉县| 铜山县| 民丰县| 贡嘎县| 客服| 西青区| 拉萨市| 凤冈县| 梅州市| 三亚市| 黄浦区| 固始县| 克山县| 专栏| 汉川市| 满洲里市| 岚皋县| 横峰县| 麦盖提县| 云梦县| 察哈| 蚌埠市| 青神县| 宽甸| 鸡西市| 聊城市| 渝北区| 易门县| 永靖县| 察雅县| 麦盖提县| 门源| 临潭县| 海南省| 太白县| 玉林市| 自贡市|