我的java代码没有编译?请帮忙?

时间:2013-03-11 11:26:26

标签: java

下面我的代码似乎没有编译,我似乎得到了9个不同的错误,请任何人都可以通过任何机会查看我的代码并发布可以使其正确运行的修改?感谢

class MineFinderModel {
    public static int MINE_SQUARE = 10;
    public static int EMPTY_SQUARE = 0;

    int num_of_cols;
    int num_of_rows;
    int[][]
        the_minefield;

    public MineFinderModel(int n_cols, int n_rows) {
        num_of_rows = n_rows;
        num_of_cols = n_cols;
        the_minefield = new int[num_of_cols][num_of_rows];
    }

    public boolean addMine(int thisCol, int thisRow) {
        if (thisCol >= n_cols || thisRow >= n_rows)
            return false;
        if (the_minefield[thisCol][thisRow] == MINE_SQUARE)
            return false;
        the_minefield[thisCol][thisRow] = MINE_SQUARE;
        return true;
    }
    public int getValue(int thisCol, int thisRow) {
        if (thisCol >= n_cols || thisRow >= n_rows)
            return false;
        return the_minefield[thisCol][thisRow];
    }
    public void addMinesToCorners() {
        the_minefield[0][0] = MINE_SQUARE;
        the_minefield[0][n_rows - 1] = MINE_SQUARE;
        the_minefield[n_cols - 1][0] = MINE_SQUARE;
        the_minefield[n_cols - 1][n_rows - 1] = MINE_SQUARE;
    }
}

3 个答案:

答案 0 :(得分:1)

很少有事情要看:
1.您没有正确定义n_colsn_rows个变量。我想您要使用num_of_colsnum_of_rows代替n_rowsn_cols
2. getValue函数假设返回int。
3.你没有Eclipse或任何其他IDE吗?

答案 1 :(得分:1)

请使用以下编辑的代码。

你的错误是构造函数中定义的参数被用作类级变量,这是错误的。变量的范围不正确。

同样在方法中:getValue你希望它返回int然后在方法的第二行返回false,这是布尔值,因此编译问题。

我已将此更改为0以返回0(检查逻辑是否未受干扰。)

我也编译了它。

class MineFinderModel {
    public static int MINE_SQUARE = 10;
    public static int EMPTY_SQUARE = 0;

    int num_of_cols;
    int num_of_rows;
    int[][] the_minefield;

    public MineFinderModel(int n_cols, int n_rows) {
        num_of_rows = n_rows;
        num_of_cols = n_cols;
        the_minefield = new int[num_of_cols][num_of_rows];
    }

    public boolean addMine(int thisCol, int thisRow) {
        if (thisCol >= num_of_cols || thisRow >= num_of_rows)
            return false;
        if (the_minefield[thisCol][thisRow] == MINE_SQUARE)
            return false;
        the_minefield[thisCol][thisRow] = MINE_SQUARE;
        return true;
    }

    public int getValue(int thisCol, int thisRow) {
        if (thisCol >= num_of_cols || thisRow >= num_of_rows)
            return 0;
        return the_minefield[thisCol][thisRow];
    }

    public void addMinesToCorners() {
        the_minefield[0][0] = MINE_SQUARE;
        the_minefield[0][num_of_rows - 1] = MINE_SQUARE;
        the_minefield[num_of_cols - 1][0] = MINE_SQUARE;
        the_minefield[num_of_cols - 1][num_of_rows - 1] = MINE_SQUARE;
    }
}

答案 2 :(得分:0)

您可以使用n_colsn_rows,而不是在addMinegetValueaddMinesToCorners方法中使用num_of_rowsnum_of_cols。因为您的其他方法无法访问n_rowsn_rows

还有一件事是,在您的以下方法中,您必须返回int而不是boolean

public int getValue(int thisCol, int thisRow) {
    if (thisCol >= num_of_cols || thisRow >= num_of_rows)
        return false; //you should return int here
    return the_minefield[thisCol][thisRow];
}