使用枚举类设置变量时出现错误消息

时间:2015-02-16 06:46:17

标签: java class enums

我正在尝试将Cell设置为我放置在枚举类中的ONE变量,但它表示" ONE无法解析或者它不是字段"当我尝试设置Cell.ONE或Cell.TWO时,我的代码中出现相同的消息,没有任何效果,但设置GameStatus没有问题......我不知道当我使用GameStatus枚举时它是如何工作的和它没有的Cell类。有任何想法吗?这是我的代码:

// No imports

private Cell[][] board;
private Cell turn;
private int size;
private GameStatus status;

public enum Cell {
    ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, EMPTY
} 

public enum GameStatus {
    PLAYER1_WON, PLAYER2_WON, PLAYER3_WON, PLAYER4_WON, PLAYER5_WON,
    PLAYER6_WON, PLAYER7_WON, PLAYER8_WON, PLAYER9_WON, TIE, 
    IN_PROGRESS
}    

public SurroundGame(int size) {
        status = GameStatus.IN_PROGRESS; 
        board = new Cell[size][size];
        turn = Cell.ONE; *ONE cannot be resolved or it is not a field*
        reset();
    }

2 个答案:

答案 0 :(得分:0)

Cell之前不应该被声明为class以发生此错误。例如,它可以是一个字段:

GameStatus Cell;

public SurroundGame(int size) {
    status = GameStatus.IN_PROGRESS; 
    board = new Cell[size][size];
    turn = Cell.ONE; /*ONE cannot be resolved or it is not a field*/
}

给出了类似的错误。

检查并仔细检查所有字段。你很可能在课堂上的某个地方找到了一个名为Cell的人。

答案 1 :(得分:0)

这是我带来的全班,以防它以某种方式帮助你,它对我运行良好...

public class Answer28535890 {

public static class SurroundGame
{
    private Cell[][] board;
    private Cell turn;
    private int size;
    private GameStatus status;

    public enum Cell {
        ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, EMPTY
    } 

    public enum GameStatus {
        PLAYER1_WON, PLAYER2_WON, PLAYER3_WON, PLAYER4_WON, PLAYER5_WON,
        PLAYER6_WON, PLAYER7_WON, PLAYER8_WON, PLAYER9_WON, TIE, 
        IN_PROGRESS
    }    

    public SurroundGame(int size) {
            status = GameStatus.IN_PROGRESS; 
            board = new Cell[size][size];
            turn = Cell.ONE; 
            reset();
    }

    public void reset()
    {

    }

    public Cell getTurn()
    {
        return turn;
    }
}

public static void main(String[] args) 
{
    SurroundGame sg = new SurroundGame(3);
    System.out.println(sg.getTurn());
}

}