簡體   English   中英

如何使用 Java 分別對二維數組的行和列求和?

[英]How to sum rows and columns of a 2D array individually with Java?

這是我正在研究的問題:編寫一個程序,讀取一個 3 x 4 矩陣並分別顯示每一列和每一行的總和。

下面是示例運行:逐行輸入一個 3×4 矩陣:

1.5 2 3 4
5.5 6 7 8
9.5 1 3 1

Sum of the elements at column 0 is 16.5
Sum of the elements at column 1 is 9.0
Sum of the elements at column 2 is 13.0
Sum of the elements at column 3 is 13.0
Sum of the elements at Row 0 is: 10.5
Sum of the elements at Row 0 is: 26.5
Sum of the elements at Row 0 is: 14.5

這是我想出的代碼:

package multidimensionalarrays;

public class MultidimensionalArrays {

    public static void main(String[] args) {
        double sumOfRow = 0;
        double[][] matrix = new double[3][4];
        java.util.Scanner input = new java.util.Scanner(System.in); //Scanner
        System.out.println("Enter a 3 by 4 matrix row by row: ");
        //Prompt user to enter matrix numbers
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[0].length; col++) {
                matrix[row][col] = input.nextDouble();
            }
        }
        double[] sumOfCol =new double[matrix[0].length];  
        for (int i = 0; i < matrix.length; i++) { //row
            for (int j = 0; j < matrix[i].length; j++) { //column
                sumOfRow += matrix[i][j];
                sumOfCol[j] += matrix[i][j];
            }
            System.out.println("Sum of the elements at row " + row + " is: " + sumOfRow);
        }
        System.out.println("Sum of the elements at column " + col + " is: " + sumOfCol);
    }             
}   

我的問題是,當打印列和行的總和結束時,它無法識別rowcol變量。 我一直在玩它並改變它可能已經幾個小時了,但我似乎無法做到這一點,有人可以幫助我解決我做錯的事情嗎? 另外我不知道我是否正確地進行了列求和?

在您的matrix中,它是來自代碼段double[][] matrix = new double[3][4]; . 第一個索引是行索引,第二個索引是列索引。

請注意,您的double[][]矩陣是arrays 的數組 matrix是三個長度為4的arrays組成的數組,按照慣例,每個子數組可以看成是排列成一行的對象數組。 這稱為行主要順序。

例如,在數組中

0 1 2 4
0 1 3 9
0 1 5 15

它實際上存儲為

  • matrix[0]: [matrix[0][0], matrix[0][1], matrix[0][2], matrix[0][3]等等[0 1 2 4]
  • matrix[1]: [matrix[1][0], matrix[1][1], matrix[1][2], matrix[1][3]等等[0 1 3 9]
  • matrix[2]: [matrix[2][0], matrix[2][1], matrix[2][2], matrix[2][3]等等[0 1 5 25]

這是一個更簡單的算法,它使用循環來查找每行和每列值的值之和,但需要兩次傳遞:

/* After the prompt code segment and sumOfCol in the main method */

    // Row (major index)
    for (int row = 0; row < matrix.length; row++) {
        int rowSum = 0;
        for (int col = 0; col < matrix[row].length; col++) {
            rowSum += matrix[row][col];
        }
        System.out.println("Sum of the elements at row " + row + " is: " + rowSum);
    }

    // Column (minor index)
    // Assuming the length of each row is the same
    for (int col = 0; col < matrix[0].length; col++) {
        int colSum = 0;
        for (int row = 0; row < matrix.length; row++) {
            colSum += matrix[row][col];
        }
        System.out.println("Sum of the elements at col " + col + " is: " + colSum);
    }

output 是

Enter a 3 by 4 matrix row by row: 
0 1 2 4
0 1 3 9
0 1 5 15
Sum of the elements at row 0 is: 7
Sum of the elements at row 1 is: 13
Sum of the elements at row 2 is: 21
Sum of the elements at col 0 is: 0
Sum of the elements at col 1 is: 3
Sum of the elements at col 2 is: 10
Sum of the elements at col 3 is: 28

在打印行總和的位置,計算行數的變量稱為i

對於列總和,您需要另一個循環來逐一打印它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM