如何创建带有二维数组的矩形?

时间:2019-02-20 17:29:39

标签: java arrays multidimensional-array rectangles drawrectangle

在本练习中,我们必须使用从10到15行和20到30列的二维数组绘制一个矩形,将矩形“#”的边界放置在矩形“-”的内部。它必须看起来像这样:https://i.stack.imgur.com/IOqY6.png

到目前为止,我的代码是这样,但是我需要一些帮助来修复它,因为我对练习有些迷茫:

public class Practica9{
public static void main(String[] args){

    char [][] tablero = new char [10][20];
    for (int i = 0; i < 10; i++){   
        for (int j = 0; j < 20; j++){
            tablero [0][19] = #;
            tablero [9][19] = #;
            System.out.println (tablero[0][19]);
            System.out.println (tablero[9][19]);
        }
    }
    for (int i = 0; i < 10; i++){
        for (int j = 0; j < 20; j++){
            tablero [1][18] = -;
            tablero [8][18] = -;
            System.out.println (tablero [1][18]);
            System.out.println (tablero [8][18]);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

public static void main(String[] args){
      int rows = 15;
      int columns =30;
      char [][] rectangle = new char[rows][columns];
      // fill array
      for(int i = 0; i < rows; i++){
            for(int j = 0; j < columns; j++){
                  if(i==0 || j==0  || i==rows-1 || j==columns-1){
                          rectangle[i][j] = '#';
                  }
                  else{
                           rectangle[i][j] = '-';
                  }
            }
      }
     // print array
      for(int i = 0; i < rows; i++){
            for(int j = 0; j < columns; j++){
                  System.out.print(rectangle[i][j]);
            }
           System.out.println();
      }
}
相关问题