我无法打印包含对象和我的 class 的数组网格?带 Java 的 Netbeans
I can't print an array grid with objects and my class? Netbeans w/ Java
我正在用扫描仪确定的数量制作网格。我不断收到错误消息,我不确定为什么。这是我要制作的网格的代码,我还将在下面包含 maze/grid 的对象和 class。
public static void mazeSetup() {
System.out.println("How many rows and columns do you want? I would\n"
+ "suggest 10 minimum and 20 maximum.");
boolean mazeselect = true;
while(mazeselect) {
maze.columns = sc.nextInt();
maze.rows = maze.columns;
if (maze.rows > 30 || maze.rows < 10) {
System.out.println("Make sure that you make it within 10-30 rows.");
} else {
mazeselect = false;
}
}
mazeBuild();
}
public static void mazeBuild() {
for(int x = 0; x < maze.rows; x++) {
for(int y = 0; y < maze.columns; y++) {
maze.maze[x][y]= ".";
System.out.print(maze.maze[x][y]);
}
System.out.println();
}
characterPlacement();
}
我这里也有对象:
static Maze maze = new Maze(null,0,0,0,0);
和 class 以及 maze/grid.
的构造函数
public class Maze {
String maze[][];
int rows;
int columns;
int xStart;
int yStart;
public Maze(String xMaze[][], int xRows, int xColumns, int xxStart, int xyStart) {
maze = xMaze;
rows = xRows;
columns = xColumns;
xStart = xxStart;
yStart = xyStart;
}
public String[][] maze() {
return maze;
}
public int rows() {
return rows;
}
public int columns() {
return columns;
}
public int xStart() {
return xStart;
}
public int yStart() {
return yStart;
}
}
如有任何帮助,我们将不胜感激。非常感谢! :D
注意:在控制台 运行 之前不会出现错误。
你的 String maze[][]
是 null
因为:
static Maze maze = new Maze(null,0,0,0,0); // notice that null
并且您正试图在调用 mazeBuild()
时将值放入其中。您应该初始化它或传递一个数组而不是 null
。您可以在 mazeBuild()
开始时执行此操作
public static void mazeBuild() {
maze.maze = new String[maze.rows][maze.columns]; // <-- this one!
for(int x = 0; x < maze.rows; x++) { // <-- this loop tries to
for(int y = 0; y < maze.columns; y++) { // put values in your
maze.maze[x][y]= "."; // maze.maze (2D String array)
System.out.print(maze.maze[x][y]);
}
System.out.println();
}
您也可以这样做以换取我添加的代码行。
String[][] mazeArray = new String[maze.rows][maze.columns];
maze = new Maze(mazeArray, maze.rows, maze.columns, 0, 0);
我正在用扫描仪确定的数量制作网格。我不断收到错误消息,我不确定为什么。这是我要制作的网格的代码,我还将在下面包含 maze/grid 的对象和 class。
public static void mazeSetup() {
System.out.println("How many rows and columns do you want? I would\n"
+ "suggest 10 minimum and 20 maximum.");
boolean mazeselect = true;
while(mazeselect) {
maze.columns = sc.nextInt();
maze.rows = maze.columns;
if (maze.rows > 30 || maze.rows < 10) {
System.out.println("Make sure that you make it within 10-30 rows.");
} else {
mazeselect = false;
}
}
mazeBuild();
}
public static void mazeBuild() {
for(int x = 0; x < maze.rows; x++) {
for(int y = 0; y < maze.columns; y++) {
maze.maze[x][y]= ".";
System.out.print(maze.maze[x][y]);
}
System.out.println();
}
characterPlacement();
}
我这里也有对象:
static Maze maze = new Maze(null,0,0,0,0);
和 class 以及 maze/grid.
的构造函数public class Maze {
String maze[][];
int rows;
int columns;
int xStart;
int yStart;
public Maze(String xMaze[][], int xRows, int xColumns, int xxStart, int xyStart) {
maze = xMaze;
rows = xRows;
columns = xColumns;
xStart = xxStart;
yStart = xyStart;
}
public String[][] maze() {
return maze;
}
public int rows() {
return rows;
}
public int columns() {
return columns;
}
public int xStart() {
return xStart;
}
public int yStart() {
return yStart;
}
}
如有任何帮助,我们将不胜感激。非常感谢! :D
注意:在控制台 运行 之前不会出现错误。
你的 String maze[][]
是 null
因为:
static Maze maze = new Maze(null,0,0,0,0); // notice that null
并且您正试图在调用 mazeBuild()
时将值放入其中。您应该初始化它或传递一个数组而不是 null
。您可以在 mazeBuild()
public static void mazeBuild() {
maze.maze = new String[maze.rows][maze.columns]; // <-- this one!
for(int x = 0; x < maze.rows; x++) { // <-- this loop tries to
for(int y = 0; y < maze.columns; y++) { // put values in your
maze.maze[x][y]= "."; // maze.maze (2D String array)
System.out.print(maze.maze[x][y]);
}
System.out.println();
}
您也可以这样做以换取我添加的代码行。
String[][] mazeArray = new String[maze.rows][maze.columns];
maze = new Maze(mazeArray, maze.rows, maze.columns, 0, 0);