1

好的,我真的需要所有这些帮助,我有一个名为 Board.java 的类,该板表示为 char 的二维数组。海龟可以通过在它通过的板上的每个位置写入一个字符来留下痕迹。董事会将有两个构造函数。默认构造函数不带参数,将创建一个 10 行 25 列的板。将板上的每个元素设置为空格。第二个构造函数将采用两个整数,分别指定行数和列数。如果指定的行数或列数小于 1,则将值设置为 1。如果指定的行数或列数大于 80,则将值设置为 80。将板中的每个元素设置为空格。该类将需要一个 clearBoard 方法。这将在每个位置放置一个空格,除了那些被海龟占据的位置。海龟使用字符“0”、“1”、“2”、...“9”来标记它们的位置。

我已经完成了以下操作,但我不确定我的构造函数是否正确,并且我不知道如何启动 clearBoard 方法。请帮忙!!

import java.util.Arrays;

public class Board {

    private char [][] theBoard;

     public Board() { // This will not take any arguments 


        theBoard = new char [10][25]; //10 rows and 25 columns
        for (int row = 0; row < theBoard.length; row++ ) {
             for (int col = 0; col < theBoard[row].length; col++ )
                 theBoard [row][col] = ' ';
    System.out.println();
        }
   }

   public Board (int [][] theBoardArray) { 
        char [][] theBoard = new char [theBoardArray.length] [theBoardArray[0].length];
        for (int row = 0; row < theBoard.length; row++ ) {
                if (row <1)
                    row = 1;
                else if (row >80)
                    row =80;
             for (int col = 0; col < theBoard[row].length; col++ ){
                if (col <1)
                    col = 1;
                else if (col >80)
                    col =80;
                 theBoard [row][col] = ' ';
            }
    System.out.println();
        }
    }
4

2 回答 2

1

修复了一些部分,清理了一下并添加了您想要的其余部分。解释在代码注释中。

public class Board {

    private char [][] theBoard;

    public Board() { // This will not take any arguments
        this(10, 25); // calls the other constructor
        // avoid duplicate code, where possible
    }

    // takes number of rows and collumns
    public Board (int rows, int cols) {
        // fix illegal row and column numbers
        rows = fixNumber(rows);
        cols = fixNumber(cols);
        // 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] = ' ';
        }
    }

    private int fixNumber(int number) {
        if (number < 1) {
            return 1;
        } else if (number > 80) {
            return 80;
        } else {
            return number;
        }
    }

    // almost like constructor, just does not create a new char[][] and
    // 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] = ' ';   
                }
            }
        }
    }
}
于 2012-12-05T23:24:17.337 回答
-1

首先,您不应该将 int 设置为 ' '。' ' 是一个字符,整数是数字。此代码将导致 int 设置为 int 32,即空格的 ascii 索引。我想如果你想考虑一个 32 的空间,这会起作用,但很奇怪。

第二个构造函数不太正确。您正在引入一个 int 数组,但您说您将采用两个 int。将行和列设置在值内的“if else”部分很好,但不是您获取它们的方式。

尝试传入两个整数,行和列,然后检查以确保它们在可接受的范围内,如果不是则更改它们,然后创建 int 数组。

于 2012-12-05T23:24:14.230 回答