难以理解将二维数组传递给方法并显示
trouble understanding passing 2d array to methods and displaying
对于这个程序,我应该使用传递方法来获取用户输入以填充 3x4 二维数组。然后添加列的总和并显示结果。
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;
}
}
当您声明 fillArray
函数时,您给它输入了 int[][] userInput
,但是当您调用时:
int[][] grid = fillArray();
你没有给出 fillArray
和 int[][]
的输入。如果您从应该修复它的 fillArray
函数中删除参数。它看起来像:
public static int[][] fillArray() {
// your code...
}
对于这个程序,我应该使用传递方法来获取用户输入以填充 3x4 二维数组。然后添加列的总和并显示结果。
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;
}
}
当您声明 fillArray
函数时,您给它输入了 int[][] userInput
,但是当您调用时:
int[][] grid = fillArray();
你没有给出 fillArray
和 int[][]
的输入。如果您从应该修复它的 fillArray
函数中删除参数。它看起来像:
public static int[][] fillArray() {
// your code...
}