2011年10月27日

          switch能夠使類(lèi)似if-else的判斷變得簡(jiǎn)明,但java中switch()不支持string,(jdk1.7支持)。
          使用枚舉可以彌補(bǔ)這一缺憾。
           1 public class SwitchTest{
           2     public enum Animal{
           3         dog,cat,mouse;
           4         public static Animal getAnimalType(String animal){
           5             return valueOf(animal.toLowerCase());
           6         }
           7     }
           8 
           9     public static void main(String[] args){
          10         selectAnimal("cat");
          11     }
          12 
          13     public static void selectAnimal(String animal){
          14         switch(Animal.getAnimalType(animal)){
          15             case cat:               
          16                 System.out.println("this is a cat");
          17                 break;
          18 
          19             case dog:               
          20                 System.out.println("this is a dog");
          21                 break;
          22 
          23             case mouse:               
          24                 System.out.println("this is a mouse");
          25                 break;
          26         }
          27     }
          28 }

          posted @ 2011-10-27 10:02 yuezt 閱讀(2631) | 評(píng)論 (0)編輯 收藏

          僅列出標(biāo)題  
          主站蜘蛛池模板: 九江县| 宁都县| 长子县| 靖州| 福海县| 延长县| 额济纳旗| 天水市| 澄江县| 潞城市| 商城县| 大竹县| 玛纳斯县| 葵青区| 崇仁县| 惠安县| 广宁县| 汉阴县| 聂拉木县| 新龙县| 北碚区| 绵阳市| 黄山市| 饶河县| 德钦县| 监利县| 永吉县| 遵义市| 桦甸市| 江永县| 廊坊市| 宝丰县| 黄大仙区| 灯塔市| 察雅县| 阳泉市| 开原市| 隆回县| 韩城市| 绥德县| 姚安县|