gpt4 book ai didi

java - 如何比较 enum 和 int?

转载 作者:行者123 更新时间:2023-12-02 00:10:40 29 4
gpt4 key购买 nike

我正在尝试使用枚举来更改颜色,我正在将枚举与 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com