/** * 考核:泛型使用;字符值匹配判断;数组集合操作 * 题目: * 1. 获取15个整型随机数1-31,存放在data中 * 2. 遍历data,判断是否重复,若重复则移除掉(使用list删除元素的方法) * 3. 使用枚举WeekEnum存放英文缩写常量和中文常量,如: MON-星期一;TUE-星期二 * 4. 题1判断星期几后输出英文缩写常量和中文常量: 如14,TUE,星期二 */ public class Main03 { public static void main(String[] args) { // 设置本月第一天为周六 Integer firstWeek = 6; Integer index = 7 - firstWeek + 1; // 算出偏移量 //随机数存放 List<Integer> data = new ArrayList<>(); Random random = new Random(); while (data.size() <= 15) { Integer num = random.nextInt(31) + 1; if (!data.contains(num)) { data.add(num); Integer day = (num - index) % 7; // 取余数 switch (day) { case 0: System.out.println(num + "," + WeekEnum.SUN.getWeekEn() + "," + WeekEnum.SUN.getWeekCn()); break; case 1: System.out.println(num + "," + WeekEnum.MON.getWeekEn() + "," + WeekEnum.MON.getWeekCn()); break; case 2: System.out.println(num + "," + WeekEnum.TUE.getWeekEn() + "," + WeekEnum.TUE.getWeekCn()); break; case 3: System.out.println(num + "," + WeekEnum.WEN.getWeekEn() + "," + WeekEnum.WEN.getWeekCn()); break; case 4: System.out.println(num + "," + WeekEnum.THIR.getWeekEn() + "," + WeekEnum.THIR.getWeekCn()); break; case 5: System.out.println(num + "," + WeekEnum.FIR.getWeekEn() + "," + WeekEnum.FIR.getWeekCn()); break; case 6: System.out.println(num + "," + WeekEnum.SAT.getWeekEn() + "," + WeekEnum.SAT.getWeekCn()); break; default: System.out.println("日期计算有误:" + num); break; } } } } }
枚举类:
public enum WeekEnum { MON("MON", "星期一"), TUE("TUE", "星期二"), WEN("WEN", "星期三"), THIR("THIR", "星期四"), FIR("FIR", "星期五"), SAT("SAT", "星期六"), SUN("SUN", "星期日"), ; private String weekEn; private String weekCn; WeekEnum(String weekEn, String weekCn) { this.weekEn = weekEn; this.weekCn = weekCn; } public String getWeekEn() { return weekEn; } public String getWeekCn() { return weekCn; } }
浙公网安备 33010602011771号