我正在尝试使用枚举来更改颜色,我正在将枚举与 int 进行比较,但它一直抛出错误
public class Game {
public enum State{
RED, YELLOW, BLANK;
}
public State getState(int x, int y) {
y=1;
for (x=5;x>0;x--) {
if (x== BLANK && y== BLANK) {
return State.RED;
}
//return State.BLANK;
}
return State.BLANK;
}
如何比较 int 和 enum?这样我就可以更改第一列 y 中设置为 1 的颜色
最佳答案
只需使用 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;
}
}
关于java - 如何比较 enum 和 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21578432/