所以,我正在尝试创建一个包含称为节点的对象的板,但出于某种原因,当我尝试创建节点数组时,我在标题中得到错误但不明白为什么。从我学到的一切,
“public static Node [] [] board = new Node [11] [11];”
应该是有效的声明。我想做的就是制作一个11x11的数组。我稍后在循环中填充数组。
我在这里和其他地方寻求帮助,但找不到任何解决问题的方法。一些想法很接近,但问题仍然存在。任何帮助都会很棒。
public class Board {
//creates the board
public static Node[][] board = new Node[11][11];
//create an empty node and place it in every other location
//like a checkered board.
for(int i = 0; i < board.length; i++) {
for (int j = 0; j< board[i].length; j+=2) {
if(i%2 == 0) {//if it is an even row start at 0
board[i][j] = new Node(null);
}else if(j+1 < board[i].length){//if odd row and less than length, start at 1
board[i][j+1] = new Node(false);
}else{
}
}
}
}
答案 0 :(得分:0)
你需要一个方法或构造函数,我不相信Java会让你把语句放在类体中。
public class Board {
// Creates the board
public static Node[][] board = new Node[11][11];
public Board() {
// Create an empty node and place it in every other location
// like a checkered board.
for(int i = 0; i < board.length; i++) {
for (int j = 0; j< board[i].length; j+=2) {
if(i%2 == 0) {
// Even row start at 0
board[i][j] = new Node(null);
}
else if(j+1 < board[i].length) {
// Odd row and less than length, start at 1
board[i][j+1] = new Node(false);
}
}
}
}
}