java构造函数和clearboard方法清除除了占用的位置之外的董事会? [英] java constructors and clearboard method which clears board except the positions occupied?

查看:47
本文介绍了java构造函数和clearboard方法清除除了占用的位置之外的董事会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok I really need help with all of this, I have a class named Board.java The board is represented as a two-dimensional array of char’s. A turtle can leave a trail by writing a character into each position on the board that it passes through. There will be two constructors for the board. The default constructor takes no arguments and will create a board with 10 rows and 25 columns. Set each element in the board to be a blank space. The second constructor will take two integers that specify the number of rows and the number of columns, respectively. If the number of rows or columns specified is below 1, set the value to 1. If the number of rows or columns specified is greater than 80, set the value to 80. Set each element in the board to be a blank space. The class will need a clearBoard method. This will put a blank space in every position, except those positions occupied by turtles. Turtles mark their positions using the characters '0', '1', '2', ... '9'.

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

I have the following completed but I am not sure my constructors are right, and I don't know how to start the clearBoard method. Help please!!

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();
        }
    }

推荐答案

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

Fixed some parts, cleaned up a bit and added the rest you want. Explanations are in code comments.

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] = ' ';   
                }
            }
        }
    }
}

这篇关于java构造函数和clearboard方法清除除了占用的位置之外的董事会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆