我试图用一个8x8 2D阵列制作一个简单的国际象棋棋盘作为我的棋子。但输出并不是我想要的,因为数组刚刚下降。 (对不起我的语言。我不太擅长编程)这是我的代码:
public class ChessBoard {
String[][] board = new String[8][8];
public void fillBoard() {
//Fills the empty spaces
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = " ";
}
}
// Rooks
board [0][0] = "R";
board [0][7] = "R";
board [7][0] = "R";
board [7][7] = "R";
// Knights
board [0][1] = "N";
board [0][6] = "N";
board [7][1] = "N";
board [7][6] = "N";
//Bishops
board [0][2] = "B";
board [0][5] = "B";
board [7][2] = "B";
board [7][5] = "B";
//Queens
board [0][3] = "Q";
board [7][3] = "Q";
//Kings
board [0][4] = "K";
board [7][4] = "K";
//Pawns
for (int i = 0; i < 8; i++) {
board[1][i] = "P";
board[6][i] = "P";
}
}
public void presentBoard() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.println(board[i][j]);
}
System.out.println();
}
}
}
输出如下:
R
N
B
Q
K
B
N
R
P
P
P
P
P
P
P
P
P
P
P
P
P
P
P
P
R
N
B
Q
K
B
N
R
但意图是这个输出:
R N B Q K B N R
P P P P P P P P
P P P P P P P P
R N B Q K B N R
我做错了什么?
编辑: 我改变了
public void presentBoard() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.println(board[i][j] + " "); //this line
}
System.out.println();
}
}
但没有任何反应。