gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:09:56 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com