繁体   English   中英

为什么我的输出搞乱了以打印二维数组?

[英]Why is my output messed up for printing out 2-d 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++) {
        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;
}

放置汽车功能应将汽车放置在指定的坐标中,并使用指定的X返回其他所有内容。 当我尝试将汽车放置在位置(2,3)时,会发生这种情况...正确放置汽车时,您可以看到带有H的位置,但是其他一些位置会消失吗?

XXXXXXXX
XXXXXXXX
XXXXXXXX
XXHXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX

H右边的五个X消失了,H下方的其他三个也消失了。 idk为什么它不能正确显示。 有任何想法吗? 谢谢

您正在将println用于汽车制造,其结果是X出现在下一行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM