我正在做一个Tic Tac Toe游戏任务,但我的棋盘并没有像我想的那样。我附上了一张图片,我希望我的董事会喜欢随着代码的一部分,我需要帮助。

我认为错误出现在代码的这一部分:
public void printBoard() {
char a = 'A';
char b = 'B';
char c = 'C';
System.out.println(" 1 2 3 ");
System.out.println(" ");
for (int i = 0; i < board.length; i++) {
if (i == 0)
System.out.print(" " + a + ' ');
else if (i == 1)
System.out.print(" " + b + ' ');
else if (i == 2)
System.out.print(" " + c + ' ');
for (int j = 0; j < board[i].length; ++j) {
System.out.print("|");
System.out.print(" " + board[i][j] + ' ');
if (j + 1 == board[i].length)
System.out.println("|");
}
System.out.println(" |---|---|---|");
}
System.out.println();
}发布于 2021-04-20 03:20:32
在我看来没问题。
public static void main(String... args) {
char[][] board = {
{ ' ', ' ', ' ' },
{ ' ', ' ', ' ' },
{ ' ', ' ', ' ' }
};
printBoard(board);
}
public static void printBoard(char[][] board) {
assert board != null;
assert board.length == 3 && board[0].length == 3 && board[1].length == 3 && board[2].length == 3;
System.out.println(" 1 2 3 ");
System.out.println(" |---|---|---|");
for (char row = 0, rowName = 'A'; row < 3; row++, rowName++) {
System.out.print(" " + rowName);
for (int col = 0; col < board[row].length; col++)
System.out.print(" | " + board[row][col]);
System.out.println(" |");
System.out.println(" |---|---|---|");
}
System.out.println();
}输出:
1 2 3
|---|---|---|
A | | | |
|---|---|---|
B | | | |
|---|---|---|
C | | | |
|---|---|---|发布于 2021-05-29 19:27:15
您可以使用打印如下内容:

public static void main(String[] args) {
char[][] board = {
{'X', ' ', ' '},
{' ', 'X', ' '},
{' ', ' ', 'O'}};
printBoard(board);
}public static void printBoard(char[][] board) {
System.out.println(" 1 2 3");
System.out.println(" ┌───┬───┬───┐");
System.out.println("A │ "
+ board[0][0] + " │ "
+ board[0][1] + " │ "
+ board[0][2] + " │ ");
System.out.println(" ├───┼───┼───┤");
System.out.println("B │ "
+ board[1][0] + " │ "
+ board[1][1] + " │ "
+ board[1][2] + " │ ");
System.out.println(" ├───┼───┼───┤");
System.out.println("C │ "
+ board[2][0] + " │ "
+ board[2][1] + " │ "
+ board[2][2] + " │ ");
System.out.println(" └───┴───┴───┘");
}输出:
1 2 3
┌───┬───┬───┐
A │ X │ │ │
├───┼───┼───┤
B │ │ X │ │
├───┼───┼───┤
C │ │ │ O │
└───┴───┴───┘https://stackoverflow.com/questions/67164993
复制相似问题