You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java机票预订系统重复选座后已选座位丢失问题求助

解决机票预订系统的座位状态保留问题

嘿,我一眼就瞅到问题出在哪了——你的results()方法每次运行时,都会把除了当前选中座位之外的所有座位重新设为*,这就导致之前选的座位全被“重置”成可用状态了。咱们来快速搞定这个问题:

核心问题分析

在你的results()方法里,这段代码是罪魁祸首:

if(row==i && col==x ){
    array[i][x]='X';
} else{
    array[i][x]='*'; // 这行直接把之前选的座位变回可用状态了!
}

每次打印座位表时,你都强制把非当前选中的座位设为*,自然就留不住之前的选择了。

修复方案

我们要把「标记已选座位」和「打印座位表」拆成两个独立步骤:选座完成后直接标记座位为已选,打印时只负责展示当前的座位状态,不再修改数组内容。

步骤1:选座完成后立即标记已选座位

修改process()方法,在switch语句结束后,添加一行代码直接标记用户选中的座位:

switch(seat){
    // 原有的case 1、2、3代码保持不变
    default:
        break;
}
// 新增这行:将用户选中的座位标记为已选(注意数组索引要减1,因为用户输入的是1开始的序号)
array[row-1][col-1] = 'X';

步骤2:修改results()方法,只负责打印

把results()里修改数组的逻辑全部删掉,只保留打印当前座位状态的代码:

static void results(){
    System.out.println("");
    System.out.println(" A B C D E F");
    num=1;
    // 移除原来的row=row-1和col=col-1操作
    for(i=0;i<13;i++){
        for(x=0; x<6; x++){
            // 移除修改数组的if-else逻辑
            if(x==0){
                if(num<=9) System.out.print(num+" ");
                else System.out.print(num+" ");
            }
            System.out.print(" "+array[i][x]);
        }
        System.out.println();
        num++;
    }
}

完整修改后的代码

import java.util.*; 
import javax.swing.JOptionPane; 
public class Exer2M{
 static Scanner in = new Scanner(System.in);
 static char array[][] = new char[14][7];
 static int num=1;
 static int k=1;
 static int row=0,col=0,O=0;
 static int i,x;
 static int seat;
 static void intro(){
 System.out.println("pick your desinated seat");
 row=1;
 System.out.println(" A B C D E F");
 for(i=0; i<13; i++){
 for(x=0; x<6; x++){
 array[i][x]='*';
 if(x==0){
 if(num<=9) System.out.print(num+" ");
 else System.out.print(num+" ");
 }
 System.out.print(" "+array[i][x]);
 }
 System.out.println();
 num++;
 }
 System.out.println("");
 System.out.println("* = seat is not taken");
 System.out.println("X = seat is taken");
 System.out.println("");
 }
 static void process(){
 do {
 System.out.print("Select a ticket type [1]First Class [2]Business Class [3]Economy Class : ");
 seat = in.nextInt();
 if (seat == 1 || seat == 2 || seat == 3) break;
 else JOptionPane.showMessageDialog(null," Invalid ticket type number", "Error",JOptionPane.ERROR_MESSAGE);
 } while (true);
 switch(seat){
 case 1:
 do {
 System.out.print("Enter Row [1-2]:");
 row= in.nextInt();
 if (row >=1 && row <=2) break;
 else JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
 } while (true);
 do {
 System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
 col = in.nextInt();
 if (col >=1 && col <=6) break;
 else JOptionPane.showMessageDialog(null," Invalid column number", "Error",JOptionPane.ERROR_MESSAGE);
 } while (true);
 break;
 case 2:
 do {
 System.out.print("Enter Row [3-7]:");
 row= in.nextInt();
 if (row >=3 && row <=7) break;
 else JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
 } while (true);
 do {
 System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
 col = in.nextInt();
 if (col >=1 && col <=6) break;
 else JOptionPane.showMessageDialog(null," Invalid column number", "Error",JOptionPane.ERROR_MESSAGE);
 } while (true);
 break;
 case 3:
 do {
 System.out.print("Enter Row [8-13]:");
 row= in.nextInt();
 if (row >=8 && row <=13) break;
 else JOptionPane.showMessageDialog(null," Invalid row number", "Error",JOptionPane.ERROR_MESSAGE);
 } while (true);
 do {
 System.out.print("Enter Column [1=A|2=B|3=C|4=D|5=E|6=F]:");
 col = in.nextInt();
 if (col >=1 && col <=6) break;
 else JOptionPane.showMessageDialog(null," Invalid column number", "Error",JOptionPane.ERROR_MESSAGE);
 } while (true);
 break;
 default:
 break;
 }
 // 标记选中座位为已选
 array[row-1][col-1] = 'X';
 }
 static void results(){
 System.out.println("");
 System.out.println(" A B C D E F");
 num=1;
 for(i=0;i<13;i++){
 for(x=0; x<6; x++){
 if(x==0){
 if(num<=9) System.out.print(num+" ");
 else System.out.print(num+" ");
 }
 System.out.print(" "+array[i][x]);
 }
 System.out.println();
 num++;
 }
 }
 static char getResponse1(String prompt) {
 char ty;
 System.out.print (prompt);
 ty = in.next().toUpperCase().charAt(0);
 return ty;
 }
 public static void main(String[]args){
 char ans;
 i = 0;
 x = 0;
 intro();
 do{
 process();
 results();
 ans = getResponse1(" Do you want to get another seat?(Y/N)?");
 } while (ans == 'Y'|| ans == 'y');
 }
 }

另外我顺手优化了两个小细节:

  • 把原来繁琐的row/col判断(比如row ==3 || row ==4...)改成了范围判断,代码更简洁;
  • 修正了列判断里多余的col==7,并把错误提示里的"row number"改成了"column number",更准确。

现在再测试一下:第一次选1E,座位表会显示1E为X;再次选2A,座位表会同时显示1E和2A为X,之前的选择就稳稳保留住啦!

内容的提问来源于stack exchange,提问作者Tray Recacho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:12:29