我正在尝试为这个程序编写一个类,这个类创建一个对象移动的板。板应该看起来像一个盒子,四个角上有“+”,“-”垂直,“|” 以空中心水平移动:
+-----+
| |
| |
| |
| |
+-----+
另一方面,我的括号在边缘水平排列,中间用逗号填充,我不知道为什么:
[ , , , , ]
[ , , , , ]
[ , , , , ]
[ , , , , ]
[ , , , , ]
我的程序是正确的,但我的课程需要帮助。
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