用于在指定位置存储字符的java方法和在某个位置返回字符的方法?

时间:2012-12-06 00:06:34

标签: java tostring multidimensional-array

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

该类需要一个setRowColumn方法。这将采用三个参数:两个整数 指定行和列,以及一个字符。该方法将字符存储在 在板上指定的位置。 该类需要一个getRowColumn方法。这将采用两个整数参数:行和 专栏。该方法将返回该位置的字符。 该类需要一个toString方法。此方法将创建一个包含该字符串的字符串 董事会的内容。电路板的每一行都是字符串中的一行。在顶部和底部使用-s绘制围绕棋盘的边框,并且每边都是|。在每个角落放一个+。 以下是我已经完成的内容,如果你可以帮我解决这两个类,请检查我的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

}

2 个答案:

答案 0 :(得分: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];
}

了解这一点,你应该能够完成剩余的漂亮印刷板。

答案 1 :(得分: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()可能无法准确地生成您想要的东西,但它还不够好。