[英]Converting int into enum type
Hello I seem to be having trouble with a project that I am working on. 您好,我似乎在为我正在研究的项目遇到麻烦。 I have created an enum type named Shift for breakfast lunch dinner swing), I then have a switch with 4 cases to choose which one it is.
我创建了一个名为Shift的枚举类型(用于早餐,午餐,晚餐,秋千),然后有一个带有4个案例的开关来选择它。
However when I try to create an object without any user input, and set the value to a random number from 1-4, there is a message that appears saying "incompatible types : int can not be converted to Shift" 但是,当我尝试创建没有任何用户输入的对象并将该值设置为1-4中的随机数时,会出现一条消息,提示“不兼容的类型:int无法转换为Shift”
any help would be appreciated! 任何帮助,将不胜感激!
EDIT* I currently have this 编辑*我目前有这个
private void getShiftAsInput()
{
Scanner cin = new Scanner(System.in);
System.out.print(" Please enter " + getName()
+ "'s Shift by Number:\n"
+ "1. Breakfast\n"
+ "2. Lunch\n"
+ "3. Dinner\n"
+ "4. Swing ========> ");
String inShift = cin.nextLine();
switch(inShift){
case "1": shift = Shift.breakfast; break;
case "2": shift = Shift.lunch; break;
case "3": shift = Shift.dinner; break;
case "4": shift = Shift.swing; break;
default: getShiftAsInput(); break;
}
}
public enum Shift { breakfast, lunch, dinner, swing }
and in the main classs am trying to do this: 在主要班级中,我试图做到这一点:
RestaurantWorker[] arr;
arr = new RestaurantWorker[6];
arr[0] = new Manager();
arr[1] = new Manager("John Edelman", "8521100900", 3, 10, 1000, 40000);
arr[2] = new WaitStaff();
arr[3] = new WaitStaff("Kelsey Green", "5662902901", 3, 36, 10.25, 40, 10, 2 );
arr[4] = new KitchenStaff();
arr[5] = new KitchenStaff("Jeff Brown", "8596042909", 2, 30, 10, 30, 0);
but am getting an error for the argument after the phone number. 但是在电话号码后面输入参数时出错。
You have to provide a way to convert int
to your target Enum
. 您必须提供一种将
int
转换为目标Enum
。
This is my way to do that: 这是我的方法:
public enum Shift {
BREAKFAST(1), LUNCH(2), DINNER(3), SWING(4);
private final int value;
public Shift(int value) {
this.value = value;
}
private static final Map<Integer, Shift> SHIFT_MAP = new HashMap<>();
static {
Arrays.stream(Shift.values()).forEach(s -> SHIFT_MAP.put(s.getValue(), s));
}
public static Shift of(Integer value) {
return SHIFT_MAP.getOrDefault(value, BREAKFAST); // Here you provide a default Shift value if input wrong value
}
}
And in your switch statement: 在您的switch语句中:
Shift shift = Shift.of(value);
switch(shift) {
case BREAKFAST:
... bla bla
}
This is how your code should be, the type mismatch is because you have to pass ENUM into the switch case. 这就是您的代码应为的方式,类型不匹配是因为您必须将ENUM传递到开关盒中。
public class Practise {
static enum SHIFT {
BREAKFAST, LUNCH, DINNER
}
public static void main(String[] args) {
SHIFT key = SHIFT.BREAKFAST; // hardcoded for example, but can be dynamic
switch (key) {
case BREAKFAST:
break;
default:
break;
}
}
} }
When you generate random integer values, you can change your logic to assign an ENUM value to the variable. 生成随机整数值时,可以更改逻辑以将ENUM值分配给变量。 Something like,
就像是,
SHIFT key = SHIFT.values()[getRandomIndex()];
You're setting an an int type to an Object Type, instead of "Shift = int" try "Shift.YourDataField = int" 您正在将int类型设置为Object Type,而不是“ Shift = int”,请尝试“ Shift.YourDataField = int”
"YourDataField" is whatever field you set in your object, and it should be an int type “ YourDataField”是您在对象中设置的任何字段,并且应为int类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.