用java实现一个枚举类,通过编号(从1开始)得到30位明星
时间: 2023-05-27 13:05:12 浏览: 159
public enum Star {
STAR1("Angelina Jolie"),
STAR2("Brad Pitt"),
STAR3("Leonardo DiCaprio"),
STAR4("Jennifer Lawrence"),
STAR5("Tom Hanks"),
STAR6("Emma Stone"),
STAR7("Johnny Depp"),
STAR8("Meryl Streep"),
STAR9("Denzel Washington"),
STAR10("Julia Roberts"),
STAR11("Will Smith"),
STAR12("Cate Blanchett"),
STAR13("George Clooney"),
STAR14("Charlize Theron"),
STAR15("Matt Damon"),
STAR16("Kate Winslet"),
STAR17("Robert De Niro"),
STAR18("Natalie Portman"),
STAR19("Hugh Jackman"),
STAR20("Anne Hathaway"),
STAR21("Sean Connery"),
STAR22("Sandra Bullock"),
STAR23("Joaquin Phoenix"),
STAR24("Audrey Hepburn"),
STAR25("Al Pacino"),
STAR26("Emma Watson"),
STAR27("Anthony Hopkins"),
STAR28("Marilyn Monroe"),
STAR29("Jim Carrey"),
STAR30("Harrison Ford");
private final String name;
private Star(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Star getByIndex(int index) {
for (Star s : Star.values()) {
if (s.ordinal() == index - 1) {
return s;
}
}
throw new IllegalArgumentException("Invalid star index: " + index);
}
}
阅读全文