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

C语言航班座位预订程序Bug咨询:满舱仍可操作及舱位切换异常

一家小型航空公司为其新的自动预订系统购置了一台计算机。总裁要求你为新系统编写程序。编写一个程序为公司唯一一架10座飞机的每个航班分配座位。程序必须显示以下选项菜单:'Please type 1 for first class'和'Please type 2 for economy.'
如果用户输入1,则程序分配头等舱座位(1至5号)。如果用户输入2,则分配经济舱座位(6至10号)。
使用一个初始化为全0的一维数组。当头等舱满员时,程序为用户提供经济舱座位(如有剩余);否则,打印'Next Flight Leaves in 3 hours.'

我的程序问题与疑问

我写的程序一直存在两个问题:

  • 所有经济舱座位被占用后,程序仍允许进入switch case 2;
  • 当向用户提供其他舱位的机票且用户同意后,程序会回到开头重新选择菜单,而非直接切换到对应的switch case处理。

我是靠教科书自学的编程,不是专家。尽管严厉评判我的代码也没关系,我不想要现成解决方案,只想知道自己错在哪里!请告诉我我的逻辑思路是否正确,还是完全理解错了问题。我试过添加更多检查和各种break,但知道这不是最简洁的解决方式,想从根源上纠正。

// TO BE REVIEWED
#include <stdio.h>
#define SEATS 10

int main(void) {
    int flightSeats[SEATS] = {0};
    int userChoice = 0;
    int firstClassSeats = 0;
    int economySeats = 0;
    int continueTravel = 1;
    char changeProposal;

    while (firstClassSeats + economySeats < SEATS && continueTravel) {
        printf("Welcome\nType 1 to travel in First Class\nType 2 to travel in Economy\n");
        scanf("%d", &userChoice);

        switch (userChoice) {
            case 1:
                printf("You have chosen to travel in First Class!\n");
                if (firstClassSeats < 5) {
                    printf("Choose your seat:\n");
                    int chosenSeat;
                    scanf("%d", &chosenSeat);

                    if (chosenSeat >= 1 && chosenSeat <= 5 && flightSeats[chosenSeat - 1] == 0) {
                        flightSeats[chosenSeat - 1]++;
                        printf("Seat no: %d assigned!\n", chosenSeat);
                        firstClassSeats++;
                    } else {
                        printf("Invalid or already occupied seat, please try again!\n");
                    }
                } else if (economySeats < 10) {
                    printf("Sorry, First Class seats are occupied\nDo you want to travel in Economy? (y or n):\n");
                    scanf(" %c", &changeProposal);

                    if (changeProposal == 'y') {
                        userChoice = 2;
                    } else if (changeProposal == 'n') {
                        printf("Thank you and goodbye!\n");
                        continueTravel = 0;
                        break;
                    } else {
                        printf("Please enter a valid character!\n");
                        getchar();
                    }
                } else {
                    printf("Sorry, all seats are occupied!\n");
                    continueTravel = 0;
                    break;
                }
                break;

            case 2:
                printf("You have chosen to travel in Economy!\n");
                if (economySeats < 10) {
                    printf("Choose your seat:\n");
                    int chosenSeat;
                    scanf("%d", &chosenSeat);

                    if (chosenSeat >= 6 && chosenSeat <= 10 && flightSeats[chosenSeat - 1] == 0) {
                        flightSeats[chosenSeat - 1]++;
                        printf("Seat no: %d assigned!\n", chosenSeat);
                        economySeats++;
                    } else {
                        printf("Invalid or already occupied seat, please try again!\n");
                    }
                } else if (firstClassSeats < 5) {
                    printf("Sorry, Economy seats are occupied\nDo you want to travel in First Class? (y or n):\n");
                    scanf(" %c", &changeProposal);

                    if (changeProposal == 'y') {
                        userChoice = 1;
                        
                    } else if (changeProposal == 'n') {
                        printf("Thank you and goodbye!\n");
                        continueTravel = 0;
                        break;
                    } else {
                        printf("Please enter a valid character!\n");
                        getchar();
                    }
                } else {
                    printf("Sorry, all seats are occupied!\n");
                    continueTravel = 0;
                    break;
                }
                break;

            default:
                printf("Please enter a valid number (1 or 2)\n");
                break;
        }
    }

    return 0;
}
你的逻辑错误点分析

1. 经济舱满员后仍能进入case 2的原因

你在case 2里判断economySeats < 10,但经济舱只有5个座位(6-10号),满员时economySeats应该等于5,而不是10。这个条件写错了,导致经济舱满员后这个判断依然成立,程序会继续执行选座逻辑,而非进入满员处理分支。

2. 用户同意换舱位后回到开头的原因

当用户同意换舱位(比如选头等舱满了同意换经济舱,你把userChoice = 2),但此时当前的switch已经执行到case 1的末尾,执行完break就跳出switch,然后回到while循环的开头重新打印菜单,不会再根据修改后的userChoice去执行对应的case。因为switch是基于进入时的userChoice值执行的,中途修改变量不会让switch重新分支。

3. 其他逻辑偏差

  • 题目要求头等舱满员且经济舱也无剩余时,打印“Next Flight Leaves in 3 hours.”,但你的代码里打印的是“Sorry, all seats are occupied!”,不符合题目要求。
  • 你用firstClassSeats和economySeats计数,这部分依赖用户选座成功才更新,逻辑没问题,但如果用户反复选无效座位,会一直循环在当前case里,直到选对或者退出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 05:54:53