设置构造函数值而不传递值

问题描述 投票:-2回答:2

我正在尝试设置setCell方法,但尚未向我提供测试文件中参数的值。 这应该使用'*'字符为我打印一个包含20行30列的框,但它正在为我打印空白行

    public Window(final int rows, final int cols, final char border)
   {
         //Initialize everything
         this.rows = rows;
         this.cols = cols;
         this.border = border;
         this.shapes = new ArrayList<Shape>();
         this.cells = new char[rows+2][cols+2];
         //Make a call to addBorders()
         addBorders(border);
   }

void setCell(final int row, final int col, final char ch)
   {
         //set the character at cells[row][col] to 'ch'
         cells[row][col]=ch;

   }


public void display()
   {
      for (int i=0; i < rows+2; i++)
      {
         for(int j=0; j<cols+2; j++)
            System.out.print(cells[i][j]);
         System.out.println();
      }
   }

我的测试器文件是=>

public class HouseForSale
{
    public static void main(String [] args) throws Exception
    {
        //Create the window
        Window w = new Window(20, 30, '*');
        w.display();
    }
}
java arrays 2d
2个回答
1
投票

您需要在构造函数(Window)中调用setrCell。如下所示:

public Window(final int rows, final int cols, final char border)
   {
         //Initialize everything
         this.rows = rows;
         this.cols = cols;
         this.border = border;
         this.shapes = new ArrayList<Shape>();
         this.cells = new char[rows+2][cols+2];

         for (int i=0; i < rows; i++)
            for(int j=0; j < cols; j++)
               setCell(i,j,ch);
         //Make a call to addBorders()
         addBorders(border);
}

0
投票

您永远不会调用setCell。使用您所拥有的内容,您应该像在显示中一样添加一个double for循环,可以类似设置某些单元格或所有单元格都作为边框获得的字符。我不太确定自己我了解您的问题。

  for (int i=0; i < rows+2; i++)
  {
     for(int j=0; j<cols+2; j++)
        setCell(i,j,border);
  }
© www.soinside.com 2019 - 2024. All rights reserved.