Java 为什么打印二维阵列时我的输出会出错?
公共类棋盘使用阵列{Java 为什么打印二维阵列时我的输出会出错?,java,multidimensional-array,Java,Multidimensional Array,公共类棋盘使用阵列{ private Car[][] myBoard = new Car[8][8]; public chessboardUsingArray() { // make sure at the end of this constructor, myBoard is 8x8 and each car // in it is set to NULL for (int x = 0; x < myBoard.length; x++) {
private Car[][] myBoard = new Car[8][8];
public chessboardUsingArray() {
// make sure at the end of this constructor, myBoard is 8x8 and each car
// in it is set to NULL
for (int x = 0; x < myBoard.length; x++) {
for (int y = 0; y < myBoard[x].length; y++) {
myBoard[x][y] = null;
// System.out.print(myBoard[x][y] + " ");
}
// System.out.println();
}
}
public void printBoard() {
for (int x = 0; x < myBoard.length; x++) {
for (int y = 0; y < myBoard[x].length; y++) {
Car defaultCar = myBoard[x][y];
if (defaultCar != null) {
System.out.println(" " + defaultCar.getMake() + " ");
} else {
System.out.print(" X ");
}
}
System.out.println();
}
}
public boolean placeCar(int y, int x, Car myCar) {
if (x >= 0 && x < 8 && y >= 0 && y < 8) {
if (myBoard[x][y] == null) {
myBoard[x][y] = myCar;
}
}
return false;
}
私家车[][]myBoard=新车[8][8];
公共棋盘使用阵列(){
//确保在此构造结束时,myBoard为8x8,每辆车
//在中,它被设置为NULL
对于(int x=0;x=0&&x<8&&y>=0&&y<8){
如果(myBoard[x][y]==null){
myBoard[x][y]=我的汽车;
}
}
返回false;
}
place car(放置汽车)功能应将汽车放置在指定的坐标中,并按指定返回带有X
的所有其他内容。当我尝试将汽车放置在位置(2,3)时,会发生这种情况…汽车被正确放置,如H
所示,但其他位置是否消失
X X X X XX X X X X
X X X X X
X X H X X
X X X X X
X X X X X
X X X X X
X X X X X X X
H右边的五个X消失了,它下面的三个也消失了。idk为什么它没有正确显示。有什么想法吗?谢谢你在汽车制造中使用了println,这会导致X出现在下一行看起来你从来没有调用过
placeCar
。。。