[英]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.