我是新來的編程初始化後空,我在我的代碼中的問題。 我首先將Maze對象初始化爲null,因爲我不知道哪個是用戶的選擇。它在createMaze()方法中初始化,但當我調用printMaze時,它是null。對象是一個方法
import java.util.Scanner;
public class Main {
public static final String EXITGAME = "EXIT";
public static final String PRINTBOARD = "PRINT";
public static final String CREATEMAZE = "UPLOAD";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Maze maze = null;
menu(maze,in);
}
private static void menu(Maze maze, Scanner in){
String option = in.nextLine();
while(option.toUpperCase() != EXITGAME){
switch(option.toUpperCase()){
case PRINTBOARD: printMaze(maze); break;
case CREATEMAZE: createMaze(in,maze); break;
}
System.out.println();
option = in.nextLine();
}
}
private static void printMaze(Maze maze){
if(maze != null){
int maxX = maze.getxSize();
int maxY = maze.getySize();
for(int x = 0; x < maxX; x++){
for(int y = 0; y < maxY; y++){
System.out.print(maze.getMazeRepresentation(x, y));
}
System.out.println();
}
}
else
System.out.println("Maze is undefined");
}
private static void createMaze(Scanner in, Maze maze){
if(maze == null || !maze.getGameStatus()){
int x = in.nextInt();
int y = in.nextInt();
in.nextLine();
char rawMaze[][] = new char[x][y];
maze = new MazeClass(x,y);
for(int i = 0; i < x; i++){
createMazeLine(in,i,y, rawMaze);
}
maze.createMaze(rawMaze);
}
else
System.out.println("Maze already defined.");
}
private static void createMazeLine(Scanner in, int lines, int y, char[][] rawMaze){
String line = in.nextLine();
for(int i = 0; i < y; i++){
rawMaze[lines][i] = line.charAt(i);
}
}
}
您做錯了。您應該利用Java的[OO](https://www.tutorialspoint.com/java/java_object_classes.htm)性質。從用戶那裏獲得迷宮的所有輸入,然後,當用戶輸入一個命令來初始化迷宮時,然後製作迷宮對象。 – cdaiga
如何,我需要等待要UPLOAD – Jffp