带toString的java Building 9x9
我有一个81个数值的列表。我正在尝试编写一个toString方法,以便它以9x9
格式打印我的值。现在,我得到的只是打印的值,直到它到达行的末尾,然后从一个新的行开始。
这是我的代码
public class Board{
public static final int Size = 9;
private Cell[][] arr = new Cell[Board.Size][Board.Size];
public Board(){
this.arr = new Cell[Board.Size][Board.Size];
for (int i=0; i<Board.Size; i++) {
for (int j=0; j<Board.Size; j++) {
arr[i][j] = new Cell(i, j, 0);
}
}
}
public String toString(){
String myBoard = new String();
myBoard = "[";
for (int i=0;i<arr.length;i++) {
for(int j=0; j<arr[i].length; j++){
myBoard += " " + arr[i][j].getValue();
}
}
myBoard += "]";
myBoard += "\n";
return myBoard;
}
这是我当前的输出:
这看起来像是一个简单的问题,但我在30分钟内还没有解决它,这就是为什么我要问这个问题
# 1 楼答案
这应该是可行的,然后像这样打印出来:
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]