计算矩阵中每列的总和会为每列返回相同的结果

时间:2014-12-06 03:57:41

标签: java for-loop

这应该将每列的所有元素相加并给出每个列的总和。当我运行它时,它只为每列提供相同的答案。我错过了什么吗?

    import java.util.Scanner;

public class SumColumnElements {
  public static void main(String[] args) {
    double[][] matrix = new double [3][4];

    Scanner input = new Scanner(System.in);
    System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns:");
    for (int row = 0;row < matrix.length; row++) {
      for (int column = 0; column < matrix[row].length; column++) {
        matrix[row][column] = input.nextDouble();
      }
    }
    sumColumn(matrix,0);
  }
  public static double sumColumn(double[][] m, int columnIndex) {
        double total = 0;
        for (int column = 0; column <= columnIndex; column++) {
          total = 0;
          for (int row = 0; row < m.length; row++) {
            total += m[row][column];
           //System.out.println("The sum for column "+column+" until row "+row+" is " + total);
          }              
         System.out.println("The sum for column "+ column + " is " + total);            

        }
        return total;
}
}

4 个答案:

答案 0 :(得分:0)

您正在紧接着打印所有值。你期望结果如何:

System.out.println("The sum for column 0 is " + total);
System.out.println("The sum for column 1 is " + total);
System.out.println("The sum for column 2 is " + total);
System.out.println("The sum for column 3 is " + total);

是?这将打印字符串后跟total四次背靠背,并且总值不会从一行变为下一行。

这应该是一行:

public static void main(String[] args) {
    double[][] matrix = new double [3][4];

    Scanner input = new Scanner(System.in);
    System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns:");
    for (int row = 0;row < matrix.length; row++) {
      for (int column = 0; column < matrix[row].length; column++) {
        matrix[row][column] = input.nextDouble();
      }
    }
    System.out.println(sumColumn(matrix,3));
  }

public static double sumColumn(double[][] m, int columnIndex) {
    double total = 0;
    for (int column = 0; column <= columnIndex; column++) {
      total = 0;
      for (int row = 0; row < m.length; row++) {
          total += m[row][column];
      }
      System.out.println("The sum for column " + column + " is " + total);
    }
    return total;
}

输入:

1 2 3 4

5 6 7 8

9 0 1 2

输出:

The sum for column 0 is 15.0
The sum for column 1 is 8.0
The sum for column 2 is 11.0
The sum for column 3 is 14.0
14.0

如果我没有指出你的方法名是&#34; sumColumn&#34;我也会失职。并且返回值为double。如果您希望此列只返回一个列的总数,则应重新考虑您传入的参数以及您对所有列进行合计的方式。

答案 1 :(得分:0)

你应该只写一次print语句,因为它会为每个列循环打印一次。其次,如果您得到相同的答案,这意味着您输出的外部for的第一个循环具有相同的值,然后您的外循环停止。检查columnIndex的值是多少。试试下面的代码

   double total = 0;
for (int column = 0; column <= columnIndex; column++) {
  total = 0;
  for (int row = 0; row < m.length; row++) {
    total += m[row][column];
  }
  System.out.println("The sum for column " +  column  + " is " + total);
}

答案 2 :(得分:0)

如果您尝试打印每列的总和(直到x行),我相信您的代码应该是什么

public static sumColumn(double[m][n] data) {

        for (int column = 0; column < m; column++) {
          // store the total value of column
          double total = 0;
          for (int row = 0; row < n; row++) {
            total += data[row][column];
           //System.out.println("The sum for column "+column+" until row "+row+" is " + total);
          }              
         System.out.println("The sum for column "+ column + is " + total);            

        }
}

更新:这应该打印每列提供双矩阵的总数。 如果要将数据返回到相应列索引

的数组total []中的callee方法存储,则不打印
public static double[] sumColumn(double[m][n] data) {

          // store the total value of column..default will be 0
          double total[] ;
        for (int column = 0; column < m; column++) {

          for (int row = 0; row < n; row++) {
            total[column] += data[row][column];
           //System.out.println("The sum for column "+column+" until row "+row+" is " + total);
          }              
         System.out.println("The sum for column "+ column + is " + total[column]);            

        }
   return total;
}

这里“0”表示第1列,依此类推,直到“m-1”表示m列

答案 3 :(得分:0)

对于大多数部分,该程序对我来说是正确的。以下是一些小调整:

import java.util.Scanner;

public class SumColumnElements {

  public static final int ROW = 3;
  public static final int COLMN = 4;

  public static void main(String[] args) {
    double[][] matrix = new double [ROW][COLMN];

    Scanner input = new Scanner(System.in);
    System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns:");
    for (int row = 0;row < matrix.length; row++) {
      for (int column = 0; column < matrix[row].length; column++) {
        matrix[row][column] = input.nextDouble();
      }
    }
    sumColumn(matrix, COLMN);
  }
  public static double sumColumn(double[][] m, int columnIndex) {
    System.out.println("columnIndex: " + columnIndex);
    double total = 0;
    for (int column = 0; column < columnIndex; column++) {
      total = 0;
      for (int row = 0; row < m.length; row++) {
        total += m[row][column];
      }
      System.out.println("The sum for column "+ column + " is " + total);
    }
    return total;
  }
}

对于特定输入:

1  2  3  4

5  6  7  8

9 10 11 12

输出将是:

The sum for column 0 is 15.0
The sum for column 1 is 18.0
The sum for column 2 is 21.0
The sum for column 3 is 24.0