求和的数组帮助行

时间:2019-06-04 15:17:08

标签: java

我的代码遇到的问题是,试图弄清楚如何获取数组中单个行的总和并打印出每一行的每个总和。

我无法获得单行的总和,它只给出总和的输出,即3。

import java.util.Scanner;

public class SumEle {

    public static double[][] sums() {
        double[][] x = new double[4][3];
        double  p;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the 3 by 4 matrix: ");
        for (int i = 0; i < x.length;i++) 
            for (int j = 0; j < x[i].length; j++)
                x[i][j] = input.nextDouble();

        return x;
    }

    public static void result(double[][] x) {
        double total = 0;
        double[] rowSum = new double[x.length];
        for(int column = 0; column< x.length; column++) {
            rowSum[column] = 0;
            for(int i = 0; i <x[column].length;i++) {
                total = total + x[column][i];
            }
            System.out.println(total);
        }
    }

    public static void main(String[] args) {
        double[][] x = sums();
        result(x);
    }
}

0 个答案:

没有答案