【问题标题】:Why won't my array fill with '?'s?为什么我的数组不会填充'?'?
【发布时间】:2014-04-17 04:45:31
【问题描述】:
    public class MakeQuilt {

public static void main (String[] args){

  char [][] myBlock = new char [4][5];
  char [][] myQuilt = new char [12][4];

  for(int row = 0; row < myBlock.length; row++){
      for(int column = 0; column < myBlock[row].length; column++){
         if(column == 0 || row == 3)
            myBlock[row][column]='X';
         else if(row ==  column || (row == 2 && column == 1))
            myBlock[row][column]='+';
         else
            myBlock[row][column]='.';    
      }   
     }
    displayPattern(myBlock);
    displayPattern(myQuilt);
  }



  public static void displayPattern(char[][] myBlock){

   for(int row = 0; row < myBlock.length; row++){
      for(int column = 0; column < myBlock[row].length; column++){
         System.out.print(myBlock[row][column]);
          }
          System.out.println();
         }
         System.out.println();    
   }

    public static void fillQuilt(char[][] myQuilt){   
   for(int row = 0; row < myQuilt.length; row++){
      for(int column = 0; column < myQuilt[row].length; column++){
         myQuilt[row][column] =('?');
        }
  }
 }
}

似乎无法弄清楚为什么我的 char 数组 myQuilt 不会填充问号而是什么都没有填充? (输出显示一堆 0)。不确定如何将 displayPattern 方法更改为在 myQuilt 数组中输出?

【问题讨论】:

  • 没有填问号是因为你没有填问号。

标签: java arrays 2d


【解决方案1】:

在致电displayPattern 之前,您必须在某处填满被子。即

displayPattern(myBlock);
fillQuilt(myQuilt);
displayPattern(myQuilt);

【讨论】:

    【解决方案2】:

    问题:您定义了fillQuilt(...) 方法,您将在其中用问号字符填充数组,但是您在哪里调用过这个方法?

    答案:你没有(至少你没有显示它),如果它从未被调用,它就永远不会做它的事情。解决方案是调用fillQuilt 方法,将myQuilt 传入您需要它执行操作的位置:fillQuilt(myQuilt);。理解编程是从字面上理解的:它们只做你明确编程它们要做的事情,不多也不少。

    【讨论】:

      【解决方案3】:

      我在您的main 中看不到对您的fillQuilt() 方法的任何调用。

      【讨论】:

        【解决方案4】:

        您不必在打印之前在某处调用 fillQuilt() 吗?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-16
          • 1970-01-01
          • 2023-02-19
          相关资源
          最近更新 更多