今天和大家分享下 在java7中可以使用String 作为switch 中的参数。
原来在java7之前,switch只能去接收一个 byte、char、short、int 类型
现在在java7中 switch 可以接收String类型啦。
下面是代码:
1 package net.cc; 2 3 /** 4 * @author test 5 * @create 2013年12月23日 下午11:34:12 6 * @version 1.0 7 */ 8 public class Test1 { 9 10 public static void printDay(String day) { 11 switch (day) { 12 case "0": 13 System.out.println("周日"); 14 break; 15 case "1": 16 System.out.println("周一"); 17 break; 18 case "2": 19 System.out.println("周二"); 20 break; 21 case "3": 22 System.out.println("周三"); 23 break; 24 case "4": 25 System.out.println("周四"); 26 break; 27 case "5": 28 System.out.println("周五"); 29 break; 30 case "6": 31 System.out.println("周六"); 32 break; 33 default: 34 System.out.println("输入有错误!"); 35 break; 36 } 37 } 38 39 public static void main(String[] args) { 40 41 printDay("1"); 42 printDay("A"); 43 } 44 }