Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算矩阵中每一列的总和会为每一列返回相同的结果_Java_For Loop - Fatal编程技术网

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

Java 计算矩阵中每一列的总和会为每一列返回相同的结果,java,for-loop,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);

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

    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;
}
}

您正在打印每个值之后的所有值。您对以下结果有何期望:

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的字符串,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;
}

如果我没有指出您的方法名是sumColumn,并且只有一个返回值double,那么我也是失职的。如果希望只返回单个列的总计,则应重新考虑传入的参数以及合计所有列的方式。

您应只编写一次打印语句,因为它将为每个列循环打印一次。第二,如果您得到相同的答案,这意味着您的输出将以相同的值为第一个外循环打印,然后您的外循环停止。检查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);
}

我相信,如果您试图打印每列的总和直到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);            

        }
}
更新:这将打印doublematrix提供的每列的总数。 如果要将数据返回到数组total[]中相应列索引处的被调用方方法存储,则不打印

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列

程序在我看来对大多数部分都是正确的。以下是一些小的调整:

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;
  }
}
输出将是:

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

有什么特别的语言吗?你把四个println命令放在彼此的后面;由于这些行之间的合计值不变,因此在每个输出中使用相同的值。应该只有一个println命令,列0,1,2,3的索引不应该被编码,而应该从列变量中获取;去掉另外3个,这就是第一列的总和,我已经知道了。我需要其他三列和。@ChristopherBasinski这是因为您的外循环没有在第一列之后运行,请检查columnIndex值是多少,因为这是循环运行的最大限制。我添加了程序的其余部分,main方法中不是说明了columnIndex吗,还是我必须移动它?@ChristopherBasinski调用sumColumnmatrix时,0传递的是columnIndex 0,它是第一个列索引。因此,外部循环在该值处停止。将其更改为sumColumnmatrix,3;谢谢你,我不应该把这个问题限制在sum方法上。这仍然只给出了1列。@ChristopherBasinski我修改了它,现在看看它
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