用java实现一个枚举类,通过编号(从1开始)得到30本世界名著(翻译成中文)
时间: 2023-05-27 21:05:45 浏览: 118
代码如下:
```
public enum WorldClassic {
ONE("鲁滨逊漂流记"),
TWO("简·爱"),
THREE("傲慢与偏见"),
FOUR("双城记"),
FIVE("了不起的盖茨比"),
SIX("哈利·波特与魔法石"),
SEVEN("红楼梦"),
EIGHT("儒林外史"),
NINE("天龙八部"),
TEN("西游记"),
ELEVEN("三国演义"),
TWELVE("水浒传"),
THIRTEEN("红与黑"),
FOURTEEN("高老庄"),
FIFTEEN("巴黎圣母院"),
SIXTEEN("罗密欧与朱丽叶"),
SEVENTEEN("悲惨世界"),
EIGHTEEN("安娜·卡列尼娜"),
NINETEEN("百年孤独"),
TWENTY("飘"),
TWENTY_ONE("战争与和平"),
TWENTY_TWO("源氏物语"),
TWENTY_THREE("人间失格"),
TWENTY_FOUR("麦田里的守望者"),
TWENTY_FIVE("奥德赛"),
TWENTY_SIX("苔丝"),
TWENTY_SEVEN("莎士比亚全集"),
TWENTY_EIGHT("教父"),
TWENTY_NINE("纳尼亚传奇"),
THIRTY("福尔摩斯探案集");
private String bookName;
WorldClassic(String bookName) {
this.bookName = bookName;
}
public String getBookName() {
return bookName;
}
public static String getBookNameByIndex(int index) {
if (index > 0 && index <= 30) {
return WorldClassic.values()[index - 1].getBookName();
} else {
return "";
}
}
}
```
使用方法:
```
String bookName = WorldClassic.getBookNameByIndex(1); //获取第一本世界名著的中文名
System.out.println(bookName); //输出:鲁滨逊漂流记
```
阅读全文