我正在尝试创建一个简单的应用程序,根据其尺寸创建2D网格然后打印它,但主方法每次都会抛出一个错误。我做错了什么?
static void int[][] createGrid(int x, int y) {
int Grid[][] = new int[x][y];
for (int a = 0; a < Grid.length; a++) {
for (int b = 0; b < Grid[a].length; a++) {
//check if there is something that currently needs to go there
//else
Grid[a][b] = 0;
}
}
}
public static void printgrid (int[][] Grid) {
for (int a = 0; a < Grid.length; a++) {
for (int b = 0; b < Grid[a].length; b++) {
System.out.print(Grid[a][b]);
}
System.out.println();
}
}
public static void main(String[] args) {
printgrid(createGrid(10, 20));
}
}
答案 0 :(得分:4)
您需要从Grid
返回createGrid
:
return Grid;
此外,您只能从createGrid
指定一种返回类型(删除void
):
static int[][] createGrid(int x, int y) {
您应该研究编译器的错误消息:它们最初可能看起来很神秘,但实际上它们会告诉您确切的问题。