用java实现一个枚举类,通过编号(从1开始)得到30位中国和韩国的明星
时间: 2023-05-27 13:05:12 浏览: 178
public enum StarEnum {
CHINA_1("赵丽颖"),
CHINA_2("杨幂"),
CHINA_3("迪丽热巴"),
CHINA_4("邓超"),
CHINA_5("周杰伦"),
CHINA_6("范冰冰"),
CHINA_7("林心如"),
CHINA_8("吴京"),
CHINA_9("姚明"),
CHINA_10("李宇春"),
KOREA_1("李秉宪"),
KOREA_2("李敏镐"),
KOREA_3("宋仲基"),
KOREA_4("金秀贤"),
KOREA_5("朴信惠"),
KOREA_6("全智贤"),
KOREA_7("李准基"),
KOREA_8("郑容和"),
KOREA_9("韩庚"),
KOREA_10("金贤重");
private String name;
StarEnum(String name) {
this.name = name;
}
public static String getStarName(int num) {
if (num < 1 || num > 20) {
throw new IllegalArgumentException("num should be between 1 and 20");
}
if (num <= 10) {
return StarEnum.valueOf("CHINA_" + num).name;
} else {
return StarEnum.valueOf("KOREA_" + (num - 10)).name;
}
}
}
阅读全文