- public enum Day
- {
- SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
- THURSDAY, FRIDAY, SATURDAY,
- NOVALUE;
- public static Day toDay(String str)
- {
- try {
- return valueOf(str);
- }
- catch (Exception ex) {
- return NOVALUE;
- }
- }
- }
- main:方法体测试
- Scanner s=new Scanner(System.in);
- String day=s.next();//输入的字符串
- switch(Day.toDay(day.toUpperCase())){
- case SUNDAY:
- System.out.println("星期天");
- break;
- case MONDAY:
- System.out.println("星期一");
- break;
- case TUESDAY:
- System.out.println("星期二");
- break;
- }
复制代码 |