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



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

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)

为了调试,这里是主菜单((;

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

显然问题是关闭扫描仪对象本身,我不明白根本原因,但代码确实在扫描器对象关闭后返回并尝试读取一行。 但是一旦我注释掉了关闭((; 扫描器对象上的语句,代码不再引发异常。

据我所知,在while循环之前,choice只从输入中读取一次,并且在每次循环迭代中保持不变。我想你应该在循环结束时添加choice = scnr.next().charAt(0);(我指的是mainMenu函数(。

另一个问题是在每个子菜单中创建一个新的扫描程序实例。

修复方式:

  • 在循环的 and 处添加choice = scnr.next().charAt(0)(使用 try-catch(
  • scnr作为参数传递给addShip函数(从函数中删除本地扫描程序,并且不关闭函数中的 scnr(

相关内容

  • 没有找到相关文章

最新更新