0

我是一名初学者 Java 编码器,我正在做课堂作业。我们必须创建一个矩形类和一个驱动程序类。该程序的重​​点是根据用户输入的行数、列数以及是否填充,用“#”符号绘制矩形。到目前为止,除了填充或未填充的逻辑之外,我拥有一切。例如,如果我放置 3 行和 3 列并且它已被填充,它将如下所示:

###
###
###

如果它是相同的东西但没有填充,它看起来像这样:

###
# #
###

请查看我的矩形类和驱动程序类,看看您是否可以提供帮助!

public class rectangle {

    private int numRows;        // The number of rows
    private int numCols;        // The number of columns
    private boolean filled;     // The boolean to determine if the rectangle is filled or unfilled


    // initialize variables
    public rectangle() { 
        numRows = 1;
        numCols = 1;
        filled = false;
    }

    /**
     * @param rows
     * @param cols
     */
    public rectangle(int numRows,int numCols){
        setRows(numRows);
        setCols(numCols);
        setFilled(filled);
    }

    // getters and setters
    public int getRows() {
        return numRows;
    }

    public void setRows(int numRows) {
        this.numRows = numRows;
    }

    public int getCols() {
        return numCols;
    }

    public void setCols(int numCols) {
        this.numCols = numCols;
    }

    public boolean getFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    // Create and return string for output
    @Override
    public String toString() {
        if(filled = true){
        for(int i = 0; i < numRows;i++){
            for(int a = 0; a < numCols;a++){
            System.out.print("#");
        }
            System.out.println();
        }
        System.out.println();
        }

        if(filled = false){
            // need this part
            }   


        return "rectangle [numRows=" + numRows + ", numCols=" + numCols
                + ", filled=" + filled + "]";
    }
}

和我的司机班:

public class Lab1ADriver {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {

        // create scanner
         Scanner kb = new Scanner(System.in);

        // create printwriter
        PrintWriter output = new PrintWriter("rectangleData.txt");

        // Ask user for input
        System.out.print("How many rectangles will you be using?");
        int numRec = kb.nextInt();
        output.println(numRec);

        // write to file
        for(int i=0; i < numRec; i++){
            System.out.print("How many rows are in the rectangle?");
            int numRows = kb.nextInt();
            System.out.print("How many columns are in the rectangle?");
            int numCols = kb.nextInt(); 
            System.out.println("Is rectangle filled? (y/n)");
            String filled = kb.next();

            // write vars to text file      
            output.println(numRows + " " + numCols + " " + filled);
        }
        // close scanner and pw
        kb.close();
        output.close();

        // array list to hold rectangle objects
        ArrayList<rectangle> box = new ArrayList<rectangle>();

        // open file for reading
        File inFile = new File("rectangleData.txt");
        Scanner recFile = new Scanner(inFile);

        // read data and add rectangle objects to array list
        recFile.nextLine();
        while(recFile.hasNext()){
            rectangle a = new rectangle();
            // read next line
            String recData = recFile.nextLine();

            // extract number of rows 
            String rowData = recData.substring(0, 1);
            rowData = rowData.trim();
            int numRows = Integer.parseInt(rowData);

            // extract columns
            String colData = recData.substring(1, 3);
            colData = colData.trim();
            int numCols = Integer.parseInt(colData);

            // extract boolean filled
            String fillData = recData.substring(3, 5);
            fillData = fillData.trim();
            char f = fillData.charAt(0);
            if (f == 'y'){
                a.setFilled(true);
            }
            else {
                a.setFilled(false) ;
            }


            a.setRows(numRows);
            a.setCols(numCols);     
            // add rectangle object to arraylist
            box.add(a);     
        }   
        recFile.close();    
        box.toString();
    }
}
4

1 回答 1

0

您可以创建一个函数,将您正在绘制的实际行和列作为输入,如果您在边界处则返回 true,否则返回 false。

如果该函数返回 true,则绘制一个“#”,如果该函数返回 false,则不绘制。

显然,只有在未填充的情况下才使用该功能。

// Create and return string for output
@Override
public String toString() {
    for(int i = 0; i < numRows;i++){
        for(int a = 0; a < numCols;a++)
        {
            if (amIAtTheBorder(i,a) || filled)
            {
                System.out.print("#");
            }
        }
        System.out.println();
    }
    System.out.println();

    ...
}
于 2013-09-02T21:52:57.430 回答