只需使用枚举的Enum.ordinal()方法获取从0到X的有序数字,您可以将其与x
变量进行比较:
public class Game {
public enum State {
BLANK, // 0
RED, // 1
YELLOW // 2
}
public State getState(int x, int y) {
y = 1;
for (x = 5; x > 0; x--) {
if (x == State.BLANK.ordinal() && y == State.BLANK.ordinal()) {
return State.RED;
}
//return State.BLANK;
}
return State.BLANK;
}
}
Run Code Online (Sandbox Code Playgroud)
您必须为枚举分配值。照原样,它试图比较两个不可比的事物。
public class Game {
public enum State{
RED(1), YELLOW(2), BLANK(0);
private int val;
private State(int value){
val = value;
}
public int getValue(){
return val;
}
}
public State getState(int x, int y) {
y=1;
for (x=5;x>0;x--) {
if (x== State.BLANK.getValue() && y== State.BLANK.getValue()) {
return State.RED;
}
//return State.BLANK;
}
return State.BLANK;
}
}
Run Code Online (Sandbox Code Playgroud)