我的java代码不是';不编译?请帮忙?
我下面的代码似乎没有编译,我似乎得到了9个不同的错误,请任何人都可以看看我的代码的任何机会,并张贴修改,可以使其正确运行?谢谢我的java代码不是';不编译?请帮忙?,java,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;
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;
}
}
要看的东西很少:1.您没有正确定义
n列
和n行
变量。我想您应该使用num\u of cols
和num\u of rows
而不是n\u rows
和n\u cols
2.
getValue
函数假定返回int.3.您没有Eclipse或任何其他IDE吗?请使用下面编辑的代码 您的错误是构造函数中定义的参数被用作类级变量,这是错误的。变量的范围不正确 同样在method:getValue中,您希望它返回int,然后在该方法的第二行中返回false,这是布尔值,因此存在编译问题 我已将其更改为返回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;
}
}
在
addMine
,getValue
,addminestockers
方法中使用num\u行
,num\u列
,而不是使用n\u列
。因为在其他方法中无法访问n_行
和n_行
还有一件事是,在下面的方法中,必须返回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];
}
这些错误是什么?提示:-检查变量声明。你似乎到处都在使用构造函数的参数名,而不是实际的实例变量。假设这是一个赋值,发布完整的代码对他没有帮助。他有逻辑,我只是纠正了他的语法错误。如果语法的更正是赋值,那么情况就不同了。我同意,但他将构造函数的参数(n_cols,n_rows)当作类字段来使用,这是一个常见的错误,告诉他这种机制会对他有更好的帮助。