[英]Print 2D array to console in board format
我试图将2D数组打印到控制台,就好像该数组是在国际象棋棋盘上的坐标一样,我的代码如下所示:
public Piece[][] getBoardView() throws NoBoardDefinedException
{
System.out.println(Arrays.deepToString(board));
return board;
}
当前,这将在控制台上以一条直线打印2D数组,有人可以提出一种将其更改为木板样式格式的方法吗?
尝试这个。
for (Piece[] row : board)
System.out.println(Arrays.toString(row));
如果是这种情况,那么您同样会得到,
int chessboard [][] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
for(int i = 0; i < chessboard.length ; i ++){
System.out.println();
for(int j = 0 ; j < chessboard[i].length ; j++){
System.out.print(" | " + chessboard[i][j] );
}
System.out.print(" |");
System.out.println();
}
输出:
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.