【问题标题】:Problems with incrementing 2-d java array递增二维 java 数组的问题
【发布时间】:2013-07-24 09:04:33
【问题描述】:

我已经尝试了三天来解决这个练习,但我不会得到它。这个想法是创建一个递增的矩阵。程序会询问行和列的大小,然后创建矩阵。

我举一个预期结果的例子:

1   2   3   4
5   6   7   8

这是我得到的:

1   1   1   1
1   1   1   1

这是我的代码:

        public static void main (String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader
                        (System.in));

        // User enters row and column size of the 2D array.
        System.out.print ("Input row size: ");
        int ro = Integer.parseInt(in.readLine());

        System.out.print ("Input column size: ");
        int co = Integer.parseInt(in.readLine());
        System.out.println ("   Array is " + ro + "x" + co);

        // Creating a 2D array.
        int[][] a = new int[ro][co];

        // User enters values, which are put into the array. 
        // This is the part where the code fails.
        System.out.print ("The matrix has " + ro*co + " integer values");
        System.out.println (" (one per line): ");
        for (int r = 0; r < a.length; r++)
            for (int c = 0; c < a[0].length; c++) {
                a [r][c] +=1;
            }

        // Printing the matrix
        System.out.println ("Matrix:");
        for (int r = 0; r < a.length; r++) {
            for (int c = 0; c < a[0].length; c++)
                System.out.print (a[r][c] + " ");
            System.out.println();
        }

        }

【问题讨论】:

    标签: java arrays matrix multidimensional-array increment


    【解决方案1】:

    你需要一个循环外的变量来增加,例如

    int incr = 0;
    

    在循环中,这样做

    a [r][c] = ++incr;
    

    目前,您正在递增数组中最初为 0 的每个元素,因此 0+1 始终以 1 结尾。

    【讨论】:

    • 感谢老兄的快速响应!
    【解决方案2】:

    您的循环只是将数组加一。由于所有数组元素都从零开始,因此所有元素都增加 1,因此您的结果。尝试在循环外包含一个变量,例如:

    int i = 0;
    

    然后将循环内的行更改为类似

    i++;
    a[r][c] = i;
    

    【讨论】:

      【解决方案3】:

      您必须在循环外递增,因为Array 是一个对象,并且任何对象的数据成员都具有默认值,因此它将0 分配给数组的每个元素。

      int inc = 0;
      

      在循环内

      a[r][c]=inc++;
      

      【讨论】:

      • 注意:当使用inc++(而不是++inc)时,矩阵中的第一个元素将是0。 (++inc 分配前递增,inc++ 分配后)
      • 这取决于你想要什么作为第一个元素,如果你想要 1 则使用前置增量,如果你想要 0 使用后置增量.. :)
      • 当然 :) 只是将此评论添加为附加信息,因为问题中的示例以 1 开头。它是针对可能阅读此内容的 Java 初学者,而不是针对您 :)
      【解决方案4】:

      public static void main(String[] args) 抛出 IOException {

          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      
          // User enters row and column size of the 2D array.
          System.out.print("Input row size: ");
          int ro = Integer.parseInt(in.readLine());
      
          System.out.print("Input column size: ");
          int co = Integer.parseInt(in.readLine());
          System.out.println("   Array is " + ro + "x" + co);
      
          // Creating a 2D array.
          int[][] a = new int[ro][co];
      
          // User enters values, which are put into the array.
          // This is the part where the code fails.
          System.out.print("The matrix has " + ro * co + " integer values");
          System.out.println(" (one per line): ");
          //For incremental purpose
          int counter = 0;
          for (int r = 0; r < a.length; r++)
              for (int c = 0; c < a[0].length; c++) {
                  a[r][c] = ++counter;
              }
      
          // Printing the matrix
          System.out.println("Matrix:");
          for (int r = 0; r < a.length; r++) {
              for (int c = 0; c < a[0].length; c++)
                  System.out.print(a[r][c] + " ");
              System.out.println();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-20
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        • 1970-01-01
        • 1970-01-01
        • 2012-09-13
        • 2019-08-08
        • 1970-01-01
        相关资源
        最近更新 更多