我正在尝试传递我的数组,但它不会打印出sumAllColumnsRows,并且我搜索了很多站点,并且不确定如何打印代码在做什么。从下面的方法传递。

public static void main(String[] args){

    Scanner input= new Scanner(System.in);

    System.out.print("Enter dimension of an nxn matrix ");
    int x=input.nextInt();
    double[][] nxn=new double[x][x];
    for(int i=0;i<x;i++) {
        System.out.print("Enter row " + (i) + ": ");
        for(int j=0;j<x;j++){
            nxn[i][j]=input.nextDouble();
        }
    }
    System.out.println(sumAllColumnsRows(m, column, rowColumn));
}

public static double sumAllColumnsRows(double[][] m, boolean column, int rowColumn)
{
    double total=0;
    for (int col = 0; col < m[0].length; col++) {
        int colSum = 0;
        for (int row = 0; row < m.length; row++) {
            colSum += m[row][col];
        }
        System.out.println("Sum of the elements at col " + col + " is: " + colSum);
    }

    for (int row = 0; row < m.length; row++) {
        int rowSum = 0;
        for (int col = 0; col < m[row].length; col++) {
            rowSum += m[row][col];
        }
        System.out.println("Sum of the elements at row " + row + " is: " + rowSum);
    }
    return total;
}

最佳答案

好的,您的问题可能在此处已确定,但我认为您可能还有其他问题。第一:

double[][] nxn=new double[x][x];
...
System.out.println(sumAllColumnsRows(m, column, rowColumn));


您正在定义变量non,但传递了m。您也没有变量column和rowColumn。我想这段代码不会编译。

但是请注意,您的sumAllColumnsRows实际上并未使用后两个参数。

来自javac的错误消息应该有助于解决这一问题。

关于java - 有人可以告诉我如何将此数组传递到我的main方法中以便打印吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58667433/

10-12 12:36
查看更多