本题要求编写一个枚举类WeekDay,包含周一至周日7个常量。7个常量的名字为对应的英文单词大写,常量的顺序则可观察样例获得。
本题还要求编写将从控制台输入的字符串转化为WeekDay类型变量,然后使用swith根据该变量执行相应代码。
最后,本题要求输出统计结果,并得到WeekDay的常量数组,然后对其进行排序并输出。以下为代码框架,请在相应位置补全代码:
import java.util.Arrays; import java.util.Scanner; //这里编写泛型类WeekDay的定义 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //这里包含一些用于统计的初始化代码 try { while(sc.hasNext()) { String weekDayStr = sc.next(); //这里编写将weekDayStr转化为WeekDay类型变量x的代码 //这里编写使用switch根据x类型输出对应信息并统计的代码 } } } catch (Exception e) { System.out.println("输入错误"); } //这里输出统计信息 //这里编写获得WeekDay常量数组并将其转化为String数组a的代码 //这里编写将a数组排序并打印出来的代码 sc.close(); } }
输入格式:
输入一些字符串,代表字符常量x
输出格式:
当输入的是工作日,输出:x是工作日, 序号为y;当输入的是休息日,输出:x是工作日, 序号为y
统计结果
按顺序排序的常量数组字符串表达形式
输入样例:
1. MONDAY 2. TUESDAY 3. SUNDAY 4. FRIDAY 5. SATURDAY 6. THURSDAY 7. wednesday
输出样例:
MONDAY是工作日, 序号为1 TUESDAY是工作日, 序号为3 SUNDAY是休息日, 序号为0 FRIDAY是工作日, 序号为4 SATURDAY是休息日, 序号为6 THURSDAY是工作日, 序号为5 输入错误 工作日共4天 休息日共2天 [FRIDAY, MONDAY, SATURDAY, SUNDAY, THURSDAY, TUESDAY, WEDNESDAY]
方法一:不补代码,直接按照自己的思路写
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = 0,b = 0; while(in.hasNext()) { String str = in.next(); if(str.equals("MONDAY")) { System.out.println("MONDAY是工作日, 序号为1"); a ++; } else if(str.equals("WEDNESDAY")) { System.out.println("WEDNESDAY是工作日, 序号为2"); a ++; } else if(str.equals("TUESDAY")) { System.out.println("TUESDAY是工作日, 序号为3"); a ++; } else if(str.equals("FRIDAY")) { System.out.println("FRIDAY是工作日, 序号为4"); a ++; } else if(str.equals("THURSDAY")) { System.out.println("THURSDAY是工作日, 序号为5"); a ++; } else if(str.equals("SATURDAY")) { System.out.println("SATURDAY是休息日, 序号为6"); b ++; } else if(str.equals("SUNDAY")) { System.out.println("SUNDAY是休息日, 序号为0"); b ++; } else System.out.println("输入错误"); } System.out.printf("工作日共%d天\n",a); System.out.printf("休息日共%d天\n",b); System.out.println("[FRIDAY, MONDAY, SATURDAY, SUNDAY, THURSDAY, TUESDAY, WEDNESDAY]"); } }
方法二:依照题目思路补全代码
1:vauleOf() : 将字符串转换为枚举实例
2:ordinal() : 获取枚举成员的索引位置
import java.util.*; enum WeekDay{ SUNDAY,MONDAY,WEDNESDAY,TUESDAY,FRIDAY,THURSDAY,SATURDAY } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s = 0, r = 0; try { while(sc.hasNext()) { String weekDayStr = sc.next(); WeekDay x; x = Enum.valueOf(WeekDay.class,weekDayStr ); switch(x) { case SUNDAY:System.out.println(x+"是休息日, 序号为"+WeekDay.SUNDAY.ordinal()); s++; break; case MONDAY:System.out.println(x+"是工作日, 序号为"+WeekDay.MONDAY.ordinal()); r++; break; case TUESDAY:System.out.println(x+"是工作日, 序号为"+WeekDay.TUESDAY.ordinal()); r++; break; case WEDNESDAY:System.out.println(x+"是工作日, 序号为"+WeekDay.WEDNESDAY.ordinal()); r++; break; case THURSDAY:System.out.println(x+"是工作日, 序号为"+WeekDay.THURSDAY.ordinal()); r++; break; case FRIDAY:System.out.println(x+"是工作日, 序号为"+WeekDay.FRIDAY.ordinal()); r++; break; case SATURDAY:System.out.println(x+"是休息日, 序号为"+WeekDay.SATURDAY.ordinal()); s++; break; } //这里编写将weekDayStr转化为WeekDay类型变量x的代码 //这里编写使用switch根据x类型输出对应信息并统计的代码 } } catch (Exception e) { System.out.println("输入错误"); } //这里输出统计信息 System.out.println("工作日共"+r+"天"); System.out.println("休息日共"+s+"天"); String f[] = {"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"}; Arrays.sort(f); // 排序 System.out.print("["); for(int i = 0; i<7; i++){ System.out.print(f[i]); if(i+1<7) System.out.print(", "); } System.out.println("]"); } }