【问题标题】:Boolean array not working properly布尔数组无法正常工作
【发布时间】:2015-07-02 11:06:44
【问题描述】:

我正在研究一个飞机上有 10 个座位的计划。前5个是头等舱和休息舱和经济舱。用户将输入一个座位号,我的程序将检查座位是否可用。如果有座位,请打印登机牌,否则请他/她选择其他座位。打印可用的座位。

在我的程序中,我有一个布尔值数组,这些值都是真的,因为有空位。每当拾取座位时,将状态更改为false。

但是,我遇到了问题。当座位被预订时,我的布尔数组变为 false,但它没有识别出座位已经被选中并将同一个座位分配给其他用户。

我真的需要帮助。我会非常感谢你们。

我的代码如下:

import java.util.Scanner;

// This program will reserve tickets for the passenger

public class Airline_Reservation 
{
// Create Arrays that will show how many tickets are available 
// Make boolean array 11 because tickets are from 1 to 10
private static boolean [] ticketsAvailable = new boolean [11];
// create a scanner
private static Scanner input = new Scanner(System.in);

// main method
public static void main(String[] args) 
{

    // initialize the array for seats
    for(int i = 1; i <= 10; i++)
    {
        // make every seat true so that it is available to book
        ticketsAvailable[i] = true;
    }

    // run a loop forever
    while(true)
    {
        // ask user to type the seat number
        System.out.println("\nPlease type the seatNumber: ");
        // get the seat number and store it in a seatNum variable
        int seatNum = input.nextInt();

        // if the seat chosen by user is available, print the boarding pass
        if (ticketsAvailable[seatNum] = true)
        {
            printBoarding(seatNum);
        }
        // if the seat chosen by user is already picked, go to shoError method
        else if (ticketsAvailable[seatNum] = false)
        {
            showError();
        }
    }

}

private static void printBoarding(int seatNumber)
{
    // This method will print the boarding pass for the user

        // check if its first class or economy class
        if (seatNumber >= 1 && seatNumber <= 5)
        {
            System.out.printf("%n%s%n%s  %s%n%s  %d%n%n",
                    "Boarding Pass", "Registered class:",
                    "First Class", "Seat Number:", seatNumber);
        }
        else if (seatNumber > 5 && seatNumber <= 10)
        {
            System.out.printf("%n%s%n%s  %s%n%s  %d%n%n",
                    "Boarding Pass", "Registered class:",
                    "Economy Class", "Seat Number:", seatNumber);
        }

        // make the seat unavailable for other people
        ticketsAvailable[seatNumber] = false;
}

private static void showError()
{
    // This method shows error when then ticket chosen is already picked

    // check the seat available and tell user if he/she wants it
    for (int i = 1; i <=10 ; i++)
    {
        // check all seats one by one and if any seat is available then prompt user t chose it
        if (ticketsAvailable[i] = true)
        {
            // check if its first class or Economy
            // First Class
            if ( i >= 1 && i <= 5)
            {
                // ask user if he/she wants the seat
                System.out.printf("%s  %s%n%s  %d%n%s",
                        "Class:", "First Class", "Seat Number:", i,
                        "Press 1 to take it or 2 to try another: ");
                int inputUser = input.nextInt(); // get user input

                // if user pick the seat
                switch(inputUser)
                {
                case 1:
                    printBoarding(i);
                    return;
                }
            }
            // Economy
            else if ( i > 5 && i <= 10)
            {
                // check if its first class or Economy
                if ( i >= 1 && i <= 5)
                {
                    // ask user if he/she wants the seat
                    System.out.printf("%s  %s%n%s  %d%n%s",
                            "Class:", "Economy Class", "Seat Number:", i,
                            "Press 1 to take it or 2 to try another: ");
                    int inputUser = input.nextInt(); // get user input

                    // if user pick the seat
                    switch(inputUser)
                    {
                    case 1:
                        printBoarding(i);
                        return;
                    }
                }
            }
        } // end if
    } // end for
}

}

谢谢!!

【问题讨论】:

  • 当您说if (ticketsAvailable[seatNum] = true) 时,您实际上将值设置为true。使用== 进行比较,使用= 进行赋值。
  • 我一直以为你用 = 来比较布尔变量。我现在会纠正它。非常非常感谢

标签: java arrays loops methods boolean


【解决方案1】:

你甚至不需要使用“==”

    // if the seat chosen by user is available, print the boarding pass
    if (ticketsAvailable[seatNum])
    {
        printBoarding(seatNum);
    }
    // if the seat chosen by user is already picked, go to shoError method
    else if (!ticketsAvailable[seatNum])
    {
        showError();
    }

因为它是一个布尔数组,所以可以直接使用if条件中的值。

【讨论】:

    【解决方案2】:

    当你在java中检查相等时你应该使用==而不是== 是一个赋值运算符。

    【讨论】:

    • 但是你不要使用 = 作为布尔变量
    • @Deep 不,你使用= 进行赋值,== 用于比较基元。
    • 没有。只要您使用本机类型,变量的类型就不会影响相等性检查。
    • @Deep 可以,但并不代表你认为的意思。第一个if 使ticketsAvailable[seatNum] 为真,并且总是成功。如果您想测试状态而不是更改状态,请使用==
    • 谢谢大家。这是我的误解。现在我得到了。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多