用java实现一个枚举类,通过编号(从1开始)得到30本世界名著(翻译成中文并且加上书名号)
时间: 2023-05-27 21:05:47 浏览: 229
```java
public enum WorldLiterature {
THE_ADVENTURES_OF_SHERLOCK_HOLMES("福尔摩斯探案集"),
PRIDE_AND_PREJUDICE("傲慢与偏见"),
TO_KILL_A_MOCKINGBIRD("杀死一只知更鸟"),
THE_GREAT_GATSBY("了不起的盖茨比"),
ANIMAL_FARM("动物农场"),
WUTHERING_HEIGHTS("呼啸山庄"),
THE_CATCHER_IN_THE_RYE("麦田里的守望者"),
THE_LITTLE_PRINCE("小王子"),
ONE_HUNDRED_YEARS_OF_SOLITUDE("百年孤独"),
WAR_AND_PEACE("战争与和平"),
THE_BROTHERS_KARAMAZOV("卡拉马佐夫兄弟"),
CRIME_AND_PUNISHMENT("罪与罚"),
ANNA_KARENINA("安娜·卡列尼娜"),
DON_QUIXOTE("堂吉诃德"),
LES_MISERABLES("悲惨世界"),
THE_IDIOT("白痴"),
FAUST("浮士德"),
THE_ODYSSEY("奥德赛"),
THE_ILIAD("伊利亚特"),
THE_DIVINE_COMEDY("神曲"),
THE_CANterbury_Tales("坎特伯雷故事集"),
THE_REMAINS_OF_THE_DAY("日落之前"),
THE_BELL_JAR("玻璃罩"),
THE_COLOR_PURPLE("紫色"),
SIDDHARTHA("悉达多"),
THE_PICTURE_OF_DORIAN_GRAY("道林·格雷的画像"),
A_TALE_OF_TWO_CITIES("双城记"),
THE_SCARLET_LETTER("红字"),
THE_TRIAL("审判");
private String name;
WorldLiterature(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static String getNameByNumber(int number) {
if (number < 1 || number > 30) {
throw new IllegalArgumentException("Invalid number: " + number);
}
return values()[number - 1].getName();
}
}
```
使用方式:
```java
String bookName = WorldLiterature.getNameByNumber(5); // 动物农场
```
阅读全文