我有一个脚本,该脚本应该是豪华邮轮旅行社的主要菜单。主菜单显示了一系列可供选择的选项,并且允许用户在它们之间进行选择。

        Luxury Ocean Cruise Outings
        System Menu

[1] Add Ship            [A] Print Ship Names
[2] Edit Ship           [B] Print Ship In Service List
[3] Add Cruise          [C] Print Ship Full List
[4] Edit Cruise         [D] Print Cruise List
[5] Add Passenger       [E] Print Cruise Details
[6] Edit Passenger      [F] Print Passenger List
[x] Exit System

Enter a menu selection:


因此,如果用户选择1或3,则会启动这些功能。问题出在这两种方法上,一旦它们完成并且程序从addShip()中断;或addCruise();它会出错,就像代码试图独自做出选择而不是等待用户输入一样,它实际上就像是回到方法本身一样。

这是完成其中一种方法然后返回到main的结果。

    Luxury Ocean Cruise Outings
        System Menu

[1] Add Ship            [A] Print Ship Names
[2] Edit Ship           [B] Print Ship In Service List
[3] Add Cruise          [C] Print Ship Full List
[4] Edit Cruise         [D] Print Cruise List
[5] Add Passenger       [E] Print Cruise Details
[6] Edit Passenger      [F] Print Passenger List
[x] Exit System

Enter a menu selection: Exception in thread "main"
Ship name: java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at luxuryV2/luxuryV2.Driver.addShip(Driver.java:233)
    at luxuryV2/luxuryV2.Driver.mainMenu(Driver.java:46)
    at luxuryV2/luxuryV2.Driver.main(Driver.java:23)


为了调试,这里是mainMenu();

    public static void mainMenu() {
        char choice = '0';
        Scanner scnr = new Scanner(System.in);
                displayMenu();
                try {
                    //scnr.next();
                    choice = scnr.next().charAt(0); // might throw an exception
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("\n\nException caught - " + e);
                }
                while (choice != 'x') {
                    if (choice == 'x') {
                        break;
                    }
                    else if (choice == '1') {
                        addShip();
                        displayMenu();
                    }
                    else if (choice == '2') {
                        editShip();
                        displayMenu();
                    }
                    else if (choice == '3') {
                        addCruise();
                        displayMenu();
                    }
                    else if (choice == '4') {
                        editCruise();
                        displayMenu();
                    }
                    else if (choice == '5') {
                        addPassenger();
                        displayMenu();
                    }
                    else if (choice == '6') {
                        editPassenger();
                        displayMenu();
                    }
                    else if (choice == 'A' || choice == 'a') {
                        printCruiseList("name");
                        displayMenu();
                    }
                    else if (choice == 'B'|| choice == 'b') {
                        printCruiseList("active");
                        displayMenu();
                    }
                    else if (choice == 'C' || choice == 'c') {
                        printCruiseList("full");
                        displayMenu();
                    }
                    else if (choice == 'D' || choice == 'd') {
                        printShipList("list");
                        displayMenu();
                    }
                    else if (choice == 'E' || choice == 'e') {
                        printShipList("details");
                        displayMenu();
                    }
                    else if (choice == 'F' || choice == 'f') {
                        printPassengerList();
                        displayMenu();
                    }
                    else {
                        System.out.println("\nSomething went wrong");
                        System.out.print("\nChoice: ");
                    }
                }
            //close scanner
            scnr.close();


这是addShip()的方法; (*请注意,它与addCruise();方法极为相似,并且出于可读性考虑,在此不再赘述。)

    public static void addShip() {
        Scanner sc = new Scanner(System.in);
        //System.out.println("\naddShip method not complete.");
        // complete this method
        //shipName; roomBalcony; roomOceanView; roomSuite; roomInterior; inService;
        Ship vessel = new Ship();
        System.out.print("\nShip name: ");
        vessel.setShipName(sc.nextLine());
        //set ship space (Balcony)
        System.out.print("How many balcony rooms: ");
        vessel.setRoomBalcony(sc.nextInt());
        //set ship ocean view rooms (int)
        System.out.print("How many Ocean Veiw rooms: ");
        vessel.setRoomOceanView(sc.nextInt());
        //set suite rooms (int)
        System.out.print("How many Suit rooms: ");
        vessel.setRoomSuite(sc.nextInt());
        //set interior rooms
        System.out.print("How many interior rooms: ");
        vessel.setRoomInterior(sc.nextInt());
        //set active service
        System.out.print("Is the Ship in active service?\n"
                + "1. Yes"
                + "\n2. No"
                + "\nChoice: ");
        int choice;
        choice = sc.nextInt();
        if (choice == 1) {
            vessel.setInService(true);
        }
        else if (choice == 2) {
            vessel.setInService(false);
        }
        else {
            System.out.println("You entered an incorrect value, setting " + vessel.getShipName() + "to Inactive.");
            vessel.setInService(false);
        }
        //add vessel to list of ships
        shipList.add(vessel);
        //close scanner.
        //sc.next();
        sc.close();

    }

最佳答案

显然问题出在关闭扫描程序对象本身,我不理解其根本原因,但是代码在关闭扫描程序对象后确实返回并尝试读取一行。但是一旦我注释掉close();扫描程序对象上的语句,代码不再引发异常。

08-18 09:20
查看更多