Java数组更新异常:购票后座位状态无法持久化
剧院座位管理程序数据不更新问题修复
问题根源
你遇到的核心问题是每次调用buy_ticket()和print_seating_area()时,都会创建一个全新的Theatre对象。这意味着购票时修改的是临时对象的座位数组,打印时读取的是另一个新对象的初始数组,两者数据完全不共享,所以打印始终显示初始值。
修复方案
1. 将静态方法改为实例方法
去掉buy_ticket()和print_seating_area()的static修饰符,让它们属于Theatre实例,保证操作的是同一个对象的座位数据。
2. 在main方法中共享一个Theatre实例
在main里创建唯一的Theatre对象,所有购票、打印操作都调用这个对象的方法。
3. 优化Scanner使用
不要每次输入都新建Scanner,全局使用一个即可,避免不必要的资源消耗。
4. 消除重复代码
提取座位选择、行打印的重复逻辑,提升代码可维护性。
修改后的完整代码
import java.util.Scanner; public class Theatre { // 座位数组,采用更简洁的命名 private int[] row1 = new int[12]; private int[] row2 = new int[16]; private int[] row3 = new int[20]; // 全局Scanner实例 private Scanner scanner = new Scanner(System.in); public void buyTicket() { System.out.println("Please select a row (1-3):"); int selectRow = scanner.nextInt(); if (selectRow == 1) { System.out.println("You have selected row 1."); System.out.println("Please select a seat (1-12):"); int selectSeat = scanner.nextInt(); handleSeatSelection(row1, selectSeat, 12); } else if (selectRow == 2) { System.out.println("You have selected row 2."); System.out.println("Please select a seat (1-16):"); int selectSeat = scanner.nextInt(); handleSeatSelection(row2, selectSeat, 16); } else if (selectRow == 3) { System.out.println("You have selected row 3."); System.out.println("Please select a seat (1-20):"); int selectSeat = scanner.nextInt(); handleSeatSelection(row3, selectSeat, 20); } else { System.out.println("Invalid row selection."); } } // 提取座位选择的重复逻辑 private void handleSeatSelection(int[] row, int seatNumber, int maxSeats) { if (seatNumber < 1 || seatNumber > maxSeats) { System.out.println("The chosen seat does not exist."); return; } int seatIndex = seatNumber - 1; if (row[seatIndex] == 0) { System.out.println("You bought a ticket"); row[seatIndex] = 1; } else { System.out.println("Seat is occupied"); } } public void printSeatingArea() { printRow(row1); printRow(row2); printRow(row3); } // 提取行打印的重复逻辑 private void printRow(int[] row) { for (int seat : row) { System.out.print(seat); } System.out.println(); } public static void main(String[] args) { System.out.println("Welcome to the New Theatre"); Theatre theatre = new Theatre(); Scanner menuScanner = new Scanner(System.in); while (true) { System.out.println("\n------------------------------------"); System.out.println("Please select an option:"); System.out.println(" 1) Buy a ticket"); System.out.println(" 2) Print seating area"); System.out.println(" 3) Cancel a ticket"); System.out.println(" 4) List available seats"); System.out.println(" 5) Save to file"); System.out.println(" 6) Load from file"); System.out.println(" 7) Print ticket information and total price"); System.out.println(" 8) Sort tickets by price"); System.out.println(" 0) Quit"); System.out.println("------------------------------------"); System.out.println("Enter option:"); int option = menuScanner.nextInt(); switch (option) { case 1: theatre.buyTicket(); break; case 2: theatre.printSeatingArea(); break; case 0: System.out.println("Exiting..."); menuScanner.close(); theatre.scanner.close(); System.exit(0); default: System.out.println("Invalid option. Please try again."); } } } }
关键修改说明
- 移除方法的
static修饰符,确保所有操作针对同一个Theatre实例的座位数据。 - 在
main中创建唯一的Theatre对象,购票、打印操作都基于该对象。 - 提取重复逻辑方法,减少代码冗余,方便后续扩展功能。
- 使用全局
Scanner实例,避免资源浪费。 - 将座位数组改为
private,符合面向对象的封装原则,防止外部直接修改数据。
内容的提问来源于stack exchange,提问作者Harry Webb
相关产品推荐
相关产品推荐

