Java 布尔数组工作不正常
我正在做一个计划,飞机上有10个座位。前五名为头等舱,其余为经济舱。用户将键入座位号,我的程序将检查座位是否可用。如果座位可用,请打印登机牌,否则请他/她选择其他座位。打印可用的座位 在我的程序中,我有一系列布尔值,它们都是真的,因为座位是可用的。每当选择座位时,将状态更改为false 但是,我遇到了一个问题。当座位被预订时,我的布尔数组将变为false,但它无法识别该座位已被拾取并将同一座位分配给其他用户 我真的需要帮助。我会非常感谢你们 我的代码如下:Java 布尔数组工作不正常,java,arrays,loops,methods,boolean,Java,Arrays,Loops,Methods,Boolean,我正在做一个计划,飞机上有10个座位。前五名为头等舱,其余为经济舱。用户将键入座位号,我的程序将检查座位是否可用。如果座位可用,请打印登机牌,否则请他/她选择其他座位。打印可用的座位 在我的程序中,我有一系列布尔值,它们都是真的,因为座位是可用的。每当选择座位时,将状态更改为false 但是,我遇到了一个问题。当座位被预订时,我的布尔数组将变为false,但它无法识别该座位已被拾取并将同一座位分配给其他用户 我真的需要帮助。我会非常感谢你们 我的代码如下: import java.util.Sc
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
}
}
import java.util.Scanner;
//这个节目将为乘客订票
公务舱航空公司预订
{
//创建显示可用票证数量的数组
//将布尔数组设为11,因为票证从1到10
私有静态布尔值[]ticketsAvailable=新布尔值[11];
//创建扫描仪
专用静态扫描仪输入=新扫描仪(System.in);
//主要方法
公共静态void main(字符串[]args)
{
//初始化席位的数组
对于(int i=1;i=1&&seatNumber 5&&seatNumber当你在java中检查相等性时,你应该使用==
而不是=
,=
是赋值操作符。当你在java中检查相等性时,你应该使用==
而不是
=
是赋值操作符。你不需要甚至需要使用“==”
由于它是一个布尔数组,您可以直接使用if条件中的值。您甚至不需要使用“=”
由于它是一个布尔数组,您可以直接使用if条件中的值。但不要使用=表示布尔值variables@Deep否,您使用
=
进行赋值,使用=
进行原语比较。否。只要您使用本机类型,变量类型不会影响相等性检查。@Deep您可以,但确实可以不是你认为的意思。第一个if
使ticketsavable[seatNum]
正确,并且始终成功。如果您想测试状态而不是更改状态,请使用=
。谢谢各位。这是我的误解。现在我明白了。感谢您的帮助,但不要使用=表示布尔值variables@Deep不,您可以使用=
进行赋值,而=
用于比较原语。不。变量的类型不是只要您使用的是本机类型,就要执行相等性检查。@您可以,但这并不意味着您认为它意味着什么。第一个if
使ticketsavable[seatNum]
正确,并且始终成功。如果您想测试状态而不是更改状态,请使用=
。谢谢各位。这是我的误解。现在我明白了。当您说如果(ticketsAvailable[seatNum]=true)时,感谢您的帮助
您实际上将值设置为true。使用=
进行比较,使用=
赋值。我一直认为您使用=进行布尔变量比较。我现在将更正它。非常感谢您说如果(ticketsAvailable[seatNum]=true)
您实际上将值设置为true。使用=
进行比较,使用=
分配值。我一直认为您使用=进行布尔变量比较。我现在将更正它。非常感谢
// 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();
}