【发布时间】:2019-11-02 01:02:34
【问题描述】:
我正在尝试传递我的数组,但它没有打印出 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。此外,在 main 方法的末尾添加对input.readLine的调用。