[英]Why is my output messed up for printing out 2-d array?
public class chessboardUsingArray { 公共类国际象棋棋盘使用数组{
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;
}
The place car function should place a car in the specified coordinates and return everything else with an X
as specified. 放置汽车功能应将汽车放置在指定的坐标中,并使用指定的X
返回其他所有内容。 When I try to put a car in location (2, 3) this happens... The car is placed correctly as you can see with an H
but do some of the other locations disappear? 当我尝试将汽车放置在位置(2,3)时,会发生这种情况...正确放置汽车时,您可以看到带有H
的位置,但是其他一些位置会消失吗?
XXXXXXXX XXXXXXXX
XXXXXXXX XXXXXXXX
XXXXXXXX XXXXXXXX
XXHXXXXX XXHXXXXX
XXXXXXXX XXXXXXXX
XXXXXXXX XXXXXXXX
XXXXXXXX XXXXXXXX
XXXXXXXX XXXXXXXX
the five X 's to the right of H disappear as do three others below it. H右边的五个X消失了,H下方的其他三个也消失了。 idk why its not being shown correctly. idk为什么它不能正确显示。 any ideas? 有任何想法吗? thanks 谢谢
您正在将println用于汽车制造,其结果是X出现在下一行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.