0

好的,伙计们,所以我试图从 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)
4

3 回答 3

1

不要在 if 条件内创建单独的新 Scanner。在 if 条件之前创建一个 Scanner 并根据您的喜好分配文件。

        int Mazenumber = myScannerinput.nextInt();
        Scanner myScanner =null;
        if(Mazenumber == 1){
            myScanner = new Scanner(new File("maze1.txt"));
        }
        else if(Mazenumber == 2){
            myScanner = new Scanner(new File("maze2.txt"));
        }
        else if(Mazenumber == 3){
            myScanner = new Scanner(new File("maze3.txt"));
        }
        else{
            System.out.println("You did not choose one of the 3 mazes");
        }
        if(myScanner!=null){
           int numRows = myScanner.nextInt();
           int numCols = myScanner.nextInt();
           myScanner.nextLine();
       }
于 2013-10-05T00:01:30.570 回答
0

代替

 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");
        }

利用

Scanner myScanner;
int Mazenumber = 0;
do {
    System.out.println("Choose 1, 2 or 3");
    Mazenumber = myScannerinput.nextInt();
    if(Mazenumber >=1 && Mazenumber <=3) {
         String fileName = "maze"+Mazenumber+".txt";
        myScanner = new Scanner(new File(fileName));
    } else {
           System.out.println("You did not choose one of the 3 mazes");      
     }

} while(Mazenumber>3 && Mazenumber <1);

Main.main(Main.java:33) 处的线程“main”java.lang.NullPointerException 中的异常

例外是因为,您已Scanner myScannerif-else块内声明。

于 2013-10-04T23:58:38.273 回答
0

您在 if 块中定义并初始化 myScanner,因此一旦您离开它就会超出范围

于 2013-10-04T23:59:06.520 回答