gpt4 book ai didi

java - 脚本正在移动过去的语句并出错

转载 作者:行者123 更新时间:2023-12-01 19:17:36 25 4
gpt4 key购买 nike

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

        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();在扫描仪对象上声明代码不再抛出异常。

关于java - 脚本正在移动过去的语句并出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59401480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com