我有以下枚举器:
public enum UserChoice {
QUIT, LIST_BOOKS, CHECKOUT_BOOK, RETURN_BOOK, LIST_MOVIES,
CHECKOUT_MOVIE, RETURN_MOVIE, USER_INFORMATION
}
我想在switch语句中使用它,它将int作为参数。但是,我需要获取枚举的int值,所以我这样做:
try {
int option = Reader.getUserOption();
} catch (InputMismatchException ex) {
option = 8;
}
switch (option) {
case UserChoice.QUIT.ordinal():
break;
case UserChoice.LIST_BOOKS.ordinal():
Printer.printBooks(library);
break;
case UserChoice.CHECKOUT_BOOK.ordinal():
// code
break;
case UserChoice.RETURN_BOOK.ordinal():
// code
break;
case UserChoice.LIST_MOVIES.ordinal():
Printer.printMovies(library);
break;
case UserChoice.CHECKOUT_MOVIE.ordinal():
// code
break;
case UserChoice.RETURN_MOVIE.ordinal():
// code
break;
case UserChoice.USER_INFORMATION.ordinal():
System.out.println(currentUser);
break;
default:
Printer.printInvalidOptionMessage();
break;
}
有没有办法将int转换为枚举值,或者我可以使用枚举实现此目的。最后,我的观点是每个案例都有枚举器的名称,这样我就可以清楚地了解每个案例的作用,因为之前我使用的是int值来完成它。
答案 0 :(得分:3)
为INVALID_CHOICE
创建一个额外的枚举值并使用它。但更重要的是,你完全将用户输入与枚举的排序(和ordinal
值)分离。
以下是如何做到这一点的一个例子。
public static enum UserChoice {
/*
* Establish the mapping between the enum value (the semantic action)
* and the user's input. This can be adapted to whatever form the user
* input takes and is decoupled from the ordinal values. It's all in
* one place here and a change here does not need a change anywhere else.
*/
QUIT ( 0),
LIST_BOOKS ( 1),
CHECKOUT_BOOK ( 2),
RETURN_BOOK ( 3),
LIST_MOVIES ( 4),
CHECKOUT_MOVIE ( 5),
RETURN_MOVIE ( 6),
USER_INFORMATION( 7),
INVALID_CHOICE (Integer.MIN_VALUE);
/*
* The mapping, and its initialization, using the new features in Java 8
*/
private static final Map<Integer,UserChoice> valueMap = Arrays.stream(UserChoice.values()).collect(Collectors.toMap(UserChoice::getValue, Function.identity()));
/*
* A method to convert from user input (int in this case) to the corresponding
* enum value based on the mapping above.
*/
public static UserChoice fromUserInput(int input) {
return Optional.ofNullable(valueMap.get(input)).orElse(INVALID_CHOICE);
}
/*
* Per-enum value and method
*/
private final int userValue;
private UserChoice(int userValue) { this.userValue = userValue; }
public int getValue() { return this.userValue; }
}
/*
* Simple test
*/
public static void main(String args[]) throws Exception
{
for (int i=0; i<10; i++)
{
UserChoice c = UserChoice.fromUserInput(i);
System.out.printf("Input %d enum is %s\n", i, c.toString());
}
}
答案 1 :(得分:2)
UserChoice.values()[option]
应该这样做。您可以单独确定option >= 0
和option < UserChoice.values().length
。
请注意,有许多资源主张反对使用或存储ordinal
,因为如果您添加,删除或重新排序枚举值,这些数字都会发生变化。如果数字是枚举的固有部分,那么RETURN_MOVIE应始终解析为选项6 - 您可能希望将其作为构造函数参数和枚举常量的属性,并通过单独的映射提供查找。
答案 2 :(得分:1)
如果Reader.getUserOption()
从UserChoice Enumerator
返回选项,则不需要演员......
switch (Reader.getUserOption()) {
case CHECKOUT_MOVIE:
break;
case LIST_BOOKS:
break;
case QUIT:
break;
default:
break;
}
当然,你需要在try-catch中进行切换案例。
如果方法getUserOption
返回一个int,那个int是枚举器中选项的序数表示:
退出,...........,USER_INFORMATION
0,...............,7
然后这样做:
switch (UserChoice.values()[Reader.getUserOption()]) {
答案 3 :(得分:0)
请尝试UserChoice.values()[option]
,其中选项从0开始,option<UserChoice.values().length
答案 4 :(得分:0)
您需要按索引查找枚举:
try {
int option = Reader.getUserOption();
} catch (InputMismatchException ex) {
option = 8;
}
switch (UserChoice.values()[option]) {
case QUIT:
break;
case LIST_BOOKS:
Printer.printBooks(library);
break;
case CHECKOUT_BOOK:
// code
break;
case RETURN_BOOK:
// code
break;
case LIST_MOVIES:
Printer.printMovies(library);
break;
case CHECKOUT_MOVIE:
// code
break;
case RETURN_MOVIE:
// code
break;
case USER_INFORMATION:
System.out.println(currentUser);
break;
default:
Printer.printInvalidOptionMessage();
break;
}
我还建议远离存储索引并将枚举名称存储为字符串,因为在枚举中添加/删除/移动值时,序数可以更改或移位。