이 클래스의 클래스를 작성하려고합니다.이 클래스는 객체가 움직이는 보드를 만듭니다. 보드는 네 모서리에 "+"가 있고 "-" 수직으로 가고 "|" 빈 중심으로 가로 :Java toString 오류
+-----+
| |
| |
| |
| |
+-----+
I를, 다른 한편으로는, 중간을 채우고 가장자리 주위 horzontally가는 괄호, 쉼표를 얻을, 나는 이유를 알고하지 않습니다
[ , , , , ]
[ , , , , ]
[ , , , , ]
[ , , , , ]
[ , , , , ]
내 프로그램을 맞습니다. 그러나 제 수업에 도움이 필요합니다.
import java.util.Arrays;
import java.util.Random;
public class Board {
private char [][] theBoard;
public Board() {
this(10, 25);
}
public Board (int rows, int cols) {
if (rows < 1 || rows>80) {
rows = 1;
}
if (cols<1 || cols > 80) {
cols = 1;
}
theBoard = new char [rows][cols];
for (int row = 0; row < theBoard.length; row++) {
for (int col = 0; col < theBoard[row].length; col++)
theBoard[row][col] = ' ';
}
}
public void clearBoard() {
for (int row = 0; row < theBoard.length; row++) {
for (int col = 0; col < theBoard[row].length; col++) {
if (theBoard[row][col] < '0' || theBoard[row][col] > '9') {
theBoard[row][col] = ' ';
}
}
}
}
public void setRowColumn(int row, int col, char character) {
theBoard[row][col] = character;
}
public char getRowColumn(int row, int col) {
return theBoard[row][col];
}
public String toString() {
StringBuilder strb = new StringBuilder();
for (char[] chars : theBoard) {
strb.append(Arrays.toString(chars) + "\n");
}
return strb.toString();
}
public static void main(String [] args)
{
Board aBoard, anotherBoard;
System.out.println("Testing default Constructor\n");
System.out.println("10 x 25 empty board:");
aBoard = new Board();
System.out.println(aBoard.toString());
System.out.println();
// And, do it again
System.out.println("Testing default Constructor again\n");
System.out.println("10 x 25 empty board:");
anotherBoard = new Board();
System.out.println(anotherBoard.toString());
} // end of method main
} // end of class
~ 해당 수정에 대한 루치아이 감사합니다. – user201535
무엇이 오류입니까? toString() 메서드를 변경하십시오. –
그래, 그게 toString 메소드와 관련이있어.하지만 찾을 수없는 것 같아.. <그냥 잘못된 문자를 출력하지만, 내가 가지고 있다고 확신했다 .. – user201535