【问题标题】:Can someone tell me how to pass this array into my main method so it prints?有人可以告诉我如何将此数组传递给我的主要方法以便打印吗?
【发布时间】: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 的调用。

标签: java arrays methods


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 谢谢,我给你们两个都投了赞成票。现在我把我的代码改成了这个......我只是不确定在 System.prnt.ln 的行中放什么 public static void main(String[] args){ Scanner input= new Scanner(System.in); System.out.print("输入一个 nxn 矩阵的维度"); int x=input.nextInt();双[][] nxn=新双[x][x]; for(int i=0;i
  • 如果您没有解决您的问题,也许可以用更易读的代码更新您的问题并让我们知道——它编译了吗?跑?怎么了?具体是什么让您感到困惑。
【解决方案2】:

您没有添加到 sumAllColumnsRows 中的总变量。它总是返回 0。

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];
        }
        total += colSum;
        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];
        }
        total += rowSum;
        System.out.println("Sum of the elements at row " + row + " is: " + rowSum);
    }
    return total;
}

【讨论】:

  • 谢谢,我给你们两个都投了赞成票。现在我把我的代码改成了这个......我只是不确定在 System.prnt.ln 的行中放什么 public static void main(String[] args){ Scanner input= new Scanner(System.in); System.out.print("输入nxn矩阵的维度"); int x=input.nextInt();双[][] nxn=新双[x][x]; for(int i=0;i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 2011-03-10
  • 1970-01-01
相关资源
最近更新 更多