真善美

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

            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 真善美 閱讀(30769) 評論(19)  編輯  收藏 所屬分類: Java學習
          主站蜘蛛池模板: 合作市| 仪征市| 肥西县| 兴海县| 赣榆县| 武鸣县| 郯城县| 济阳县| 利川市| 拜泉县| 乌拉特前旗| 浦县| 武强县| 读书| 十堰市| 宁河县| 井冈山市| 阳城县| 徐闻县| 隆林| 德州市| 上林县| 游戏| 同江市| 广昌县| 禄丰县| 策勒县| 西城区| 佛教| 疏附县| 偏关县| 沈阳市| 三河市| 伊春市| 翼城县| 繁峙县| 遵义市| 中江县| 福安市| 铁力市| 会理县|