好的,伙计们,所以我试图从 3 个文件中的 1 个文件中读取,通常没有if
...else
子句
只是Scanner myScanner = new Scanner(new File("maze1.txt"));
会运行问题,但我希望用户选择他/她想要运行的迷宫
public class Main {
private static Scanner myScanner;
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Please Enter 1,2 or 3 to pick the different maze you would like being solved.");
Scanner myScannerinput = new Scanner(System.in);
int Mazenumber = myScannerinput.nextInt();
if(Mazenumber == 1){
Scanner myScanner = new Scanner(new File("maze1.txt"));
}
else if(Mazenumber == 2){
Scanner myScanner = new Scanner(new File("maze2.txt"));
}
else if(Mazenumber == 3){
Scanner myScanner = new Scanner(new File("maze3.txt"));
}
else{
System.out.println("You did not choose one of the 3 mazes");
}
int numRows = myScanner.nextInt();
int numCols = myScanner.nextInt();
myScanner.nextLine();
int startX = 0;
int startY = 0;'
请输入 1,2 或 3 选择您想要解决的不同迷宫。
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:33)