预订机票

时间:2019-02-19 14:59:19

标签: java

我正在尝试制作一个基本的Java Airline Reservation App。这是我的代码,似乎在我按下“ 1”之后,它终止了并且没有运行其余的代码。我不确定我的循环是否有问题或为什么终止。如果有人有任何想法或答案,请随时听听!谢谢

import java.util.Scanner;

public class Reservation {

    boolean[] seat = new boolean[11];
    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() {

        for (int count = 1; count <= 5; count++) {
            if (seat[count] = false) {
                seat[count] = true;
                System.out.printf("First Class. Seat# %d\n", count);
                break;
            } else if (seat[10] == true) {
                if (seat[5] == true) {

                } else {
                    System.out.println("First class is fully booked, would you like an econmy seat");
                    int choice = input.nextInt();
                    if (choice == 1) {
                        firstClassSeat();
                        start();
                    } else {
                        System.out.println("Next flight is in 3 hours");
                        System.exit(0);
                    }
                }
            }
        }

    }

    public void economySeat() {
        for (int count = 6; count <= 10; count++) {
            if (seat[count] = false) {
                seat[count] = true;
                System.out.printf("First Class. Seat# %d\n", count);
                break;
            } else if (seat[10] == true) {
                if (seat[5] == true) {

                } else {
                    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();
                        start();
                    } else {
                        System.out.println("Next flight is in 3 hours");
                        System.exit(0);
                    }
                }
            }
        }

    }

}

1 个答案:

答案 0 :(得分:1)

首先,不需要在if中比较布尔值。 seats[i] == false!seats[i]相同。

什么都没有发生,因为您有一个分配:if(seat[count]=false,而不是比较:if(seat[count]==false。这是第一个错误,修复之后,您将开始分配座位。

接下来,当我认为您想调用firstClassSeat时(当第一堂课已满时),您正在同一方法内调用economySeat。您需要修正检查和经济/头等舱建议背后的逻辑。