java - 有人可以告诉我如何将此数组传递到我的 main 方法中以便打印吗?

标签 java arrays methods

我正在尝试传递我的数组,但它没有打印出 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/

相关文章:

java - Hibernate Criteria API orderBy 和嵌入元素

java - 从输入中查找多数元素

javascript - 如何在 typescript 中创建一个零数组?

java - 难以理解类对象和构造函数

oop - 返回类型的具体类型或接口(interface)?

java - 客户端套接字接受的数据与服务器套接字不同

java - Tomcat中context.xml获取资源的方法

javascript - 如何使用javascript将数字划分为数组中包含的特定值

java - 给定一个包含多个重复条目的数组,找到一个重复条目 O(N) 时间和常数空间

java - 程序编译但未正确写入文件