-1

わかりましたので、次の方法について本当に助けが必要です。開始方法が本当にわかりません。

クラスには setRowColumn メソッドが必要です。これには 3 つの引数が必要です。行と列を指定する 2 つの整数と、1 つの文字です。このメソッドは、ボード上の指定された場所に文字を保存します。クラスには getRowColumn メソッドが必要です。これは、行と列の 2 つの整数引数を取ります。このメソッドは、その位置にある文字を返します。クラスには toString メソッドが必要です。このメソッドは、ボードの内容を含む文字列を作成します。ボードの各行は、文字列の 1 行になります。ボードの上下に - を使用し、両側に | を使用して、ボードの周囲に境界線を引きます。各コーナーに + を入れます。以下は私が完成したものです.2つのクラスで私を助けてくれれば、私の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

ここで物事を過度に複雑にしようとしていると思います。ボードは 2 次元配列であるため、すべてのポジションに直接アクセスできます。

したがって、特定の行/列に文字を設定するには、次のようにします。

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 に答える