【问题标题】:trouble understanding passing 2d array to methods and displaying难以理解将二维数组传递给方法和显示
【发布时间】:2021-12-14 23:04:29
【问题描述】:

有了这个程序,我应该使用传递的方法来获取用户输入来填充一个 3x4 2d 数组。然后将各列的总和相加并显示结果。

int[][] grid = fillArray();有一个 in[][] 需要的错误。为什么我无法在 main 中调用我的方法?这就是本书所说的与无数 youtube 视频一起做的方式。

public class SumOfColumns {

    

    public static int sumColumn(int[][] m, int columnIndex) {
        for (int column = 0; column < 4; column++) {
            columnIndex = 0;
            for (int row = 0; row < 3; row++) {
                columnIndex += m[column][row];
            }
        }
        return columnIndex;
    }

    public static void main(String[] args) {
        
       ***int[][] grid = fillArray();***
    }
    public static int[][] fillArray(int[][] userInput) {
        Scanner input = new Scanner(System.in);
         int[][] grid = new int[3][4];
        System.out.println("Enter a 3x4 grid of numbers please: ");
        for (int row = 0; row < grid.length; row++) {
            for (int column = 0; column < grid[row].length; column++) {
                grid[row][column] = input.nextInt();
            }
        }
        return grid;
    }

}

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

    当你声明你的fillArray 函数时,你给了它一个int[][] userInput 的输入,但是当你调用时:

    int[][] grid = fillArray();
    

    您不提供fillArrayint[][] 的输入。如果您从应该修复它的 fillArray 函数中删除参数。它看起来像:

    public static int[][] fillArray() {
        // your code...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多