Java简易机票预订程序异常终止求助:输入1后程序退出
Fixing Your Java Flight Booking App Termination Issue
Hey there! Let's figure out why your app terminates unexpectedly when selecting first class (input 1) and get it working smoothly. There are a few key bugs in your code causing this behavior—here's the breakdown and fixes:
Key Issues Identified
- Incorrect Operator Usage: You used assignment (
=) instead of comparison (==) when checking seat availability. This completely breaks the seat booking logic and leads to unexpected behavior. - Flawed Full-Seat Handling: Recursive calls to
start()and misordered checks for full cabins cause either early termination or potential stack overflow. - Logical Flow Errors: Full cabin checks happen inside the seat loop instead of after verifying all seats are taken, so the app never properly triggers the "full cabin" prompt when it should.
- Typos in Output: The economy seat method incorrectly prints "First Class" when assigning an economy seat, which is a minor but confusing mistake.
Corrected Code
import java.util.Scanner; public class Reservation { boolean[] seat = new boolean[11]; // Indexes 1-10 used for seats Scanner input = new Scanner(System.in); public void start() { while (true) { makeReservation(); } } public void makeReservation() { System.out.println("Please type 1 for first class and type 2 for economy"); int section = input.nextInt(); if (section == 1) { firstClassSeat(); } else { economySeat(); } } public void firstClassSeat() { boolean firstClassFull = true; // Check all first class seats for availability for (int count = 1; count <= 5; count++) { if (!seat[count]) { // Clean way to check if seat is unoccupied seat[count] = true; System.out.printf("First Class. Seat# %d\n", count); firstClassFull = false; break; } } // Handle full first class scenario if (firstClassFull) { System.out.println("First class is fully booked, would you like an economy seat? (1 for Yes, 2 for No)"); int choice = input.nextInt(); if (choice == 1) { economySeat(); } else { System.out.println("Next flight is in 3 hours"); System.exit(0); } } } public void economySeat() { boolean economyFull = true; // Check all economy seats for availability for (int count = 6; count <= 10; count++) { if (!seat[count]) { seat[count] = true; System.out.printf("Economy. Seat# %d\n", count); // Fixed typo here economyFull = false; break; } } // Handle full economy scenario if (economyFull) { System.out.println("Economy is fully booked. Would you like First Class? (1 for Yes, 2 for No)"); int choice = input.nextInt(); if (choice == 1) { firstClassSeat(); } else { System.out.println("Next flight is in 3 hours"); System.exit(0); } } } public static void main(String[] args) { Reservation bookingApp = new Reservation(); bookingApp.start(); } }
Key Fixes Explained
- Fixed Seat Check: Changed
seat[count] = falseto!seat[count]to correctly check if a seat is unoccupied (the original assignment was overriding seat status before any proper check). - Rearranged Full Cabin Logic: Now we first scan all seats in the cabin, set a flag if it's full, then handle the "offer other cabin or next flight" logic after the loop. This ensures we only trigger the full cabin prompt when all seats are truly taken.
- Removed Unnecessary Recursion: Got rid of the recursive
start()call—sincestart()already runs an infinite loop, we just need to call the other cabin method directly when the user chooses to switch. - Fixed Output Typo: Corrected the economy seat message to show "Economy" instead of "First Class".
- Added Main Method: Included a
mainmethod to actually launch the app (your original code was missing this, which might have caused confusion when running the program).
Now when you select first class, the app will correctly assign an available seat, or offer economy if first class is full—without terminating unexpectedly.
内容的提问来源于stack exchange,提问作者N. Norris
相关产品推荐
相关产品推荐

