数组问题。代码看起来错误的输出

时间:2018-03-06 18:35:16

标签: java arrays

此代码看起来正确。只是我没有得到我想要的结果。我试图找到3x2 2D阵列中每列的总和,我得到了这个。 代码如下:

import java.util.Random;

public class testa {

    public static void main(String[] args) {
        double[][] m = new double[3][2];
        //int b = 0;
        Random rand = new Random();

        double sum = 0;

        createArray();

        printResult(m, sum);
    }
    public static double[][] createArray() {
        double[][] bucky = new double[3][2];
        Random rand = new Random();
        for (int row = 0; row < bucky.length; row++) {

            for (int column = 0; column < bucky[row].length; column++) {

                bucky[row][column] = rand.nextInt(50);


                System.out.print(bucky[row][column] + "  ");
            }
            System.out.println();
            //return createArray();
        }
        return bucky;
    }

    public static double sumColumn(double[][] bucky, int columnIndex) {
        int sum = 0;
        ColumnIndex = 1
        for (int i = 0; i < bucky.length; i++) {
            sum += bucky[i][columnIndex];
            System.out.println(bucky[i][columnIndex] + "\n");

        }
        System.out.print(sum + "\n");

        return sum;
    }
    public static void printResult(double[][] bucky, double sum) {
        //System.out.printf("       ");
        for (int i = 0; i < bucky.length; i++) {

            System.out.print(sumColumn(bucky, i) + "   ");

        }
        System.out.println();
    }

}

输出:

28.0  18.0  
12.0  9.0  
36.0  23.0  
0
0
0
0.0   0
0
0
0.0   0 
0
0
0.0   

同样很高兴找到如何在阵列中获得20-50之间的随机数。

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些问题:

  • double[][] m = new double[3][2]创建一个所有值均为0的数组,然后调用createArray()但不会修改m
  • 当您致电printResult(m, sum)时,您的m仍然会被零填充
  • 在您的printResult(double[][] bucky, double sum)中,您没有使用sum所以它可以只是printResult(double[][] bucky)
  • sumColumn(double[][] bucky, int columnIndex)中,您设置了columnIndex = 1,因此您总是将第二列汇总。它需要删除
  • sumColumn(double[][] bucky, int columnIndex)中,你的循环不正确,你在bucky.length上循环,这将在行上循环,你需要取第一行(bucky [0])并循环它的长度:for (int i = 0; i < bucky[0].length; i++)
  • 最后你的日志很难阅读,我改了它们,所以我们可以看到发生了什么。

这是您的代码,清除了这些错误:

public static void main(String[] args) {
  // create your array using your createArray method
  double[][] m = createArray();
  printResult(m);
}

public static double[][] createArray() {
  System.out.println("Create Array");
  double[][] bucky = new double[3][2];
  Random rand = new Random();
  for (int row = 0; row < bucky.length; row++) {
    for (int column = 0; column < bucky[row].length; column++) {
      bucky[row][column] = rand.nextInt(50);
      System.out.print(bucky[row][column] + "  ");
    }
    System.out.println();
  }
  return bucky;
}


public static void printResult(double[][] bucky) {
  System.out.println("Print Result");
  for (int i = 0; i < bucky[0].length; i++) {
    System.out.println("Column "+i);
    System.out.println(" => " + sumColumn(bucky, i));
  }
  System.out.println();
}

public static double sumColumn(double[][] bucky, int columnIndex) {
  int sum = 0;
  for (int i = 0; i < bucky.length; i++) {
    sum += bucky[i][columnIndex];
    System.out.print(bucky[i][columnIndex] + " ");
  }
  System.out.print(" => " + sum);

  return sum;
}

打印出来:

Create Array
13.0  25.0  
18.0  18.0  
23.0  38.0  
Print Result
Column 0
13.0 18.0 23.0  => 54 => 54.0
Column 1
25.0 18.0 38.0  => 81 => 81.0