好的,所以我真的需要以下方法的帮助,我真的不知道如何开始。
该类将需要一个 setRowColumn 方法。这将采用三个参数:两个整数来指定行和列,以及一个字符。该方法将字符存储在板上的指定位置。该类将需要一个 getRowColumn 方法。这将采用两个整数参数:行和列。该方法将返回该位置的字符。该类将需要一个 toString 方法。此方法将创建一个包含棋盘内容的字符串。棋盘的每一行将是字符串中的一行。使用 - 在顶部和底部绘制边框,并在每侧使用 |。在每个角落放一个+。以下是我完成的,如果你能帮助我完成这两个类,请检查我的 toString 方法。谢谢你。
public class Board {
private char [][] theBoard;
public Board() { // This will not take any arguments
this(10, 25); // calls the other constructor
}
// takes number of rows and columns
public Board (int rows, int cols) {
// fix illegal row and column numbers
if (rows < 1 || rows>80) {
rows = 1;
}
if (cols<1 || cols > 80) {
cols = 1;
}
// create the board and fill it with ' '
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] = ' ';
}
}
// only puts ' ' into fields not containing any of '0' - '9'
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) {
int index = 0;
for ( int i = 1; i <= row; i++ )
if ( col[i] > col[index] )
index = i;
return index;
}
public String toString() { //begin toString method
int i;
String temp = new String (""); //create string
// drawLine
String line = "";
char topBottom = '-';
int k;
int row2 = theBoard[0].length;
for ( k = 0 ; k < row2 ; k++ ){
line += topBottom; // adding hyphens
}
System.out.println('+' + line + '+'); // adding left and right + corners
for (i=0; i<theBoard.length; i++) {
temp += "|"; //add characters to the string
for (int j=0; j<theBoard[0].length; j++) {
temp = temp + theBoard[i][j] + "|"; //add the actual number to the string
}
temp += "\n";
}
return line + "\n" + temp + "\n" + line; //return string
}// end of method toString
}