为什么我的代码打印 printf 语句 6 次?

问题描述 投票:0回答:1

对于舱位,我必须在头等舱(前 3 个座位)和经济舱(第 4 至 6 个座位)之间随机分配飞机上的 6 个座位。我必须使用一个分配座位的功能和一个单独的功能来打印单行“登机牌”,说明分配的座位。我无法分配已分配的座位,如果其中一个班级已满,我将提供另一种座位类型。如果座位被拒绝,我必须打印出下一趟航班将在 3 小时后起飞。我尝试了几种不同的方法,但似乎无法消除有关每个人是否登机的额外五个提示。这是我的代码:

PS 我不被允许在这段代码中使用许多先进的技术。我很确定我只是在某个地方的流量控制错误。

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int search_seat(int a[]);
int print_boarding_pass(int a[], int seat_type, int seat_number);

int main(void) {
    int a[6] = { 0 };
    search_seat(a);
    while (a[0] == 0 || a[1] == 0 || a[2] == 0 || a[3] == 0 || a[4] == 0 || a[5] == 0) {
        search_seat(a);
    }
}

int search_seat(int a[]) {
    int seat_number = 0;
    int seat_type = 0;
    if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 2 && a[4] == 2 && a[5] == 2) {
        printf("Next flight leaves in 3 hours\n");
        return -1;
    }
    printf("please type 1 for 'firstclass'\nPlease type 2 for 'economy'\n");
    scanf("%d", &seat_type);
    int no_seat;
    if (seat_type == 1) {
        if (a[0] == 1 && a[1] == 1 && a[2] == 1) {
            printf("no seat available in first class, do you want a seat in economy? ");
            scanf("%d", &no_seat);
            if (no_seat == 0) {
                printf("Next flight leaves in 3 hours\n");
                print_boarding_pass(a, 0, seat_number);
            }
            else if (no_seat == 1) {
                srand(time(NULL));
                seat_type = 2;
                seat_number = rand() % (6 - 3) + 3;
                while (a[seat_number] == 2) {
                    seat_number = rand() % (6 - 3) + 3;
                }
                a[seat_number] = 2;
                printf("array is %d %d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
                print_boarding_pass(a, seat_type, seat_number);
            }
        }
        srand(time(NULL));
        seat_number = 0 + rand() % 3;
        while (a[seat_number] == 1) {
            seat_number = 0 + rand() % 3;
        }
        a[seat_number] = 1;
        printf("array is %d %d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
        print_boarding_pass(a, seat_type, seat_number);
    }
    if (seat_type == 2) {
        if (a[3] == 2 && a[4] == 3 && a[5] == 2) {
            printf("no seat available in economy, do you want a seat in first class? ");
            scanf("%d", &no_seat);
            if (no_seat == 0) {
                printf("Next flight leaves in 3 hours\n");
                print_boarding_pass(a, 0, seat_number);
            }
            else if (no_seat == 1) {
                srand(time(NULL));
                seat_type = 1;
                seat_number = 0 + rand() % 3;
                while (a[seat_number] == 1) {
                    seat_number = 0 + rand() % 3;
                }
                a[seat_number] = 1;
                printf("array is %d %d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
                print_boarding_pass(a, seat_type, seat_number);
            }
        }
        srand(time(NULL));
        seat_number = rand() % (6 - 3) + 3;
        while (a[seat_number] == 2) {
            seat_number = rand() % (6 - 3) + 3;
        }
        a[seat_number] = 2;
        printf("array is %d %d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
        print_boarding_pass(a, seat_type, seat_number);
    }
}

int print_boarding_pass(int a[], int seat_type, int seat_number) {
    if (seat_type == 1) {
        printf("Your seat is assigned to first class seat %d\n", (seat_number + 1));
        search_seat(a);
    }
    if (seat_type == 2) {
        printf("Your seat is assigned to economy seat %d\n", (seat_number + 1));
        search_seat(a);
    }

    printf("Is everone boarded? ");
    int boarded;
    scanf("%d", &boarded);
    if (boarded == 0) {
        search_seat(a);
    }
    if (boarded == 1) {
        return -1;
    }
}

编译器读取:

please type 1 for 'firstclass'
Please type 2 for 'economy'
1
array is 1 0 0 0 0 0
Your seat is assigned to first class seat 1
please type 1 for 'firstclass'
Please type 2 for 'economy'
1
array is 1 1 0 0 0 0
Your seat is assigned to first class seat 2
please type 1 for 'firstclass'
Please type 2 for 'economy'
1
array is 1 1 1 0 0 0
Your seat is assigned to first class seat 3
please type 1 for 'firstclass'
Please type 2 for 'economy'
1
no seat available in first class, do you want a seat in economy? 1
array is 1 1 1 0 0 2
Your seat is assigned to economy seat 6
please type 1 for 'firstclass'
Please type 2 for 'economy'
2
array is 1 1 1 0 2 2
Your seat is assigned to economy seat 5
please type 1 for 'firstclass'
Please type 2 for 'economy'
2
array is 1 1 1 2 2 2
Your seat is assigned to economy seat 4
Next flight leaves in 3 hours`your text`
Is everone boarded? 1
Is everone boarded? 1
Is everone boarded? 1
c output printf
1个回答
0
投票

如果您的意思是“为什么用户输入提示的输出是:

“请输入 1 代表‘头等舱’

请输入 2 代表“经济””

出现了 6 次,原因是你正在使用 while 循环来检查数组中的每个元素是“0”还是“1”。

 while (a[0] == 0 || a[1] == 0 || a[2] == 0 || a[3] == 0 || a[4] == 0 || a[5] == 0) {
        search_seat(a);
    }

它检查数组的每个元素是否为“0”,如果是则运行循环。

也不需要一次又一次地要求用户订票,而是可以询问用户“您想再次订票”,如果是,您可以提示。

© www.soinside.com 2019 - 2024. All rights reserved.