-1

好的,所以我真的需要以下方法的帮助,我真的不知道如何开始。

该类将需要一个 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

}
4

2 回答 2

1

我认为您在这里试图使事情过于复杂。由于您的棋盘是二维数组,我们可以直接访问所有位置。

因此,将字符设置为特定的行/列,如下所示:

public void setRowColumn(int row, int col, char character) {
    theBoard[row][col] = character;
}

并以相反的方式获取特定字符:

public char getRowColumn(int row, int col) {
    return theBoard[row][col];
}

知道了这一点,您应该能够完成电路板的其余漂亮打印。

于 2012-12-06T00:12:39.013 回答
0
public void setRowColumn(int row, int col, char character) {
    try {
        theBoard[row][col] = character;
    } catch (ArrayOutOfBoundsException e) {
        e.printStackTrace();
        // what should happen when one provides illegal indexes?
    }
}

@Override
public String toString() {
    StringBuilder strb = new StringBuilder();
    for (char[] chars : theBoard) {
        strb.append(Arrays.toString(chars) + "\n");
    }
    return strb.toString();
}

toString()可能不会产生你想要的东西,恕我直言,它已经足够好了。

于 2012-12-06T00:13:56.130 回答