我有一个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分钟没有弄明白了,这就是我为什么要问的原因。
发布于 2020-06-30 06:52:54
这个应该可以像这样打印出来:
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
String myBoard = "";
for (int i = 0; i < arr.length; i++) {
myBoard += "[";
for(int j = 0; j < arr[i].length; j++){
myBoard += " " + arr[i][j].getValue();
}
myBoard += "]";
myBoard += "\n";
}
https://stackoverflow.com/questions/62651718
复制相似问题