2014-12-06 43 views
0

這應該是將每列的所有元素相加並給出每列的總和。當我運行它時,它對每列都給出相同的答案。我錯過了什麼嗎?計算矩陣中每列的總和會爲每列返回相同的結果

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

任何特定的語言? – 2014-12-06 03:59:01

+0

我正在使用DrJava。 – 2014-12-06 03:59:58

+0

您將四個println命令放在一起,因爲'total'在這些行之間沒有變化,所以每個輸出都使用相同的值。應該只有一個println命令,並且列(0,1,2,3)的索引不應該被編入,而是從'column'變量中獲取。 – 2014-12-06 04:06:18

回答

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 

如果我沒有指出你的方法名是「sumColumn」,並且有一個單一的返回值double,我也會失職。如果你希望這隻返回一列的總數,你應該重新考慮你傳遞的參數以及總計所有列的方式。

0

您應該只寫一次打印語句,因爲它將爲每個列循環打印一次。其次,如果您得到的答案相同,也就是說您輸出的第一個循環外部輸入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); 
} 
+0

這給出了第一列的總和,這是我已經。我需要另外三列總和。 – 2014-12-06 04:16:08

+0

@ChristopherBasinski那是因爲你的外循環沒有在第一列之後運行,請檢查什麼是'columnIndex'值,因爲這是循環運行的最大限制。 – Abhay 2014-12-06 04:17:27

+0

我添加了程序的其餘部分,是不是主要方法中聲明的columnIndex,還是我必須移動它? – 2014-12-06 04:25:44

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

     } 
} 

更新你的代碼應該是什麼:這應該打印總量爲每列提供了一個雙基陣。 相反,如果你想返回數據中的被調用方法存儲在陣列中總[]各自的列索引

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」的打印裝置,第一列等等,直到「M-1」是指米列

+0

這仍然只給出1列。 – 2014-12-06 04:23:23

+0

@ChristopherBasinski我修改了它現在看看它 – 2014-12-06 05:00:53

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 
相關問題