我对Java还是很陌生,但是我在c ++和python方面有不错的经验。因此,我正在做一个问题,即即时通讯需要实施飞机预订系统,该系统可以执行以下操作-
1。将所有座位初始化为未占用(假)
2。要求输入(生态或一流)
3。检查座位是否未被占用
4。如果没有座位,请分配座位,否则寻找下一个座位
5。如果预订了经济舱座位,请询问用户是否想升至头等舱
6。如果用户为否定,则显示味精“下一平面在3小时内”
但是
package oop;
import java.util.Arrays;
import java.util.Scanner;
public class AirplaneBooking {
private final static int MAX_ECO_SEATS = 5;
private final static int MAX_FIRST_CLASS_SEATS = 3;
private final static boolean[] ECO_SEATS = new boolean[MAX_ECO_SEATS];
private final static boolean[] FIRST_CLASS_SEATS = new boolean[MAX_FIRST_CLASS_SEATS];
private static int current_eco_seat = 0;
private static int current_first_class_seat = 0;
public static void initialilze_seats(boolean[] first_class_seats, boolean[] eco_class_seats){
Arrays.fill(first_class_seats, Boolean.FALSE);
Arrays.fill(eco_class_seats, Boolean.FALSE);
}
public static void display(boolean[] seats){
System.out.print("[");
for(boolean seat : seats){
System.out.print(seat + ",");
}
System.out.println("]");
}
public static void book_seat(boolean [] seats, int current_seat){
seats[current_seat] = true;
current_seat++;
System.out.println(current_seat);
}
public static int user_input() {
Scanner input = new Scanner(System.in);
System.out.print("Enter 1 for Economy class or 2 for First class : ");
int user_seat_prefrence = input.nextInt();
if (user_seat_prefrence == 1){
if(current_eco_seat < MAX_ECO_SEATS){
book_seat(ECO_SEATS, current_eco_seat);
}
else{
System.out.println("Looks like eco seats are full, would you like to book for first class insted(1/0) ?");
Scanner next_input = new Scanner(System.in);
int user_next_seat_prefrence = next_input.nextInt();
if (user_next_seat_prefrence == 1){
book_seat(FIRST_CLASS_SEATS, current_first_class_seat);
user_seat_prefrence = 2;
}
else{
System.out.println("next flight leaves in 3 hrs");
}
}
}
else if (user_seat_prefrence == 2){
if (current_first_class_seat < MAX_FIRST_CLASS_SEATS){
book_seat(FIRST_CLASS_SEATS, current_first_class_seat);
}
else{
System.out.println("Looks like first class seats are full, would you like to book economy instead?(1/0)");
int user_next_seat_prefrence = input.nextInt();
if (user_next_seat_prefrence == 1){
book_seat(ECO_SEATS, current_eco_seat);
user_seat_prefrence = 1;
}
else{
System.out.println("Next flight leaves in 3hrs");
}
}
}
else {
System.out.println("Enter valid option");
}
return user_seat_prefrence;
}
public static void print_boarding_pass(int user_seat_prefrence){
if (user_seat_prefrence == 1){
System.out.println("eco");
System.out.println(current_eco_seat - 1);
}
else{
System.out.println("first class");
System.out.println(current_first_class_seat - 1);
}
}
public static void main(String args[]){
initialilze_seats(FIRST_CLASS_SEATS, ECO_SEATS);
display(FIRST_CLASS_SEATS);
display(ECO_SEATS);
while(true){
int user_seat_prefrence = user_input();
print_boarding_pass(user_seat_prefrence);
display(FIRST_CLASS_SEATS);
display(ECO_SEATS);
System.out.print("book another seat:");
Scanner choice = new Scanner(System.in);
boolean book_another_seat = choice.nextBoolean();
if (book_another_seat == false)
break;
}
}
}
此代码存在的问题是,例如,生态舱位(例如)是否已满,该程序应该询问我是否要预订头等舱并等待输入,如果按1
应该在头等舱预定,但是程序不会等待我的输入,而是继续执行else
语句。
此外,我使用static
变量current_eco_seat
和current_first_class_seat
来跟踪当前预订的座位,并将该静态变量传递给book_seat
函数,然后,程序会预订座位并递增current_eco_seat
或current_first_class_seat
(取决于通过哪个座位),以便可以在下一次交互中预定下一个座位。但是静态变量根本不会增加。
这些是我程序唯一的问题。
感谢您的帮助,谢谢
答案 0 :(得分:1)
当Java通过值调用方法时,
关于静态的问题是您将current_seat
的值传递给book_seat
方法,因此更改值不会影响从该方法返回后的变量。
要解决此问题,只需调用方法,而不传递您的静态变量。它是静态的,因此您可以从任何地方访问它。
即:
public static void book_seat(boolean [] seats){
seats[current_seat] = true;
current_first_class_seat++;
System.out.println(current_seat);
}
答案 1 :(得分:0)
不确定您的问题是否与“静态”变量有关,或更与“如何处理输入流?”有关。
关于:
如果我按1,它应该在头等舱预定,但是程序不会等待我的输入,而是继续执行else语句。
在重新阅读之前,您应该考虑“刷新”输入流。 flush-clear-system-in-stdin-before-reading
另一方面,这种方法是错误的
public static void book_seat(boolean [] seats, int current_seat){
seats[current_seat] = true;
current_seat++;
System.out.println(current_seat);
}
此命令无效,但会向用户打印信息。您在调用方“ current_eco_seat”中使用的变量完全不会更改。
答案 2 :(得分:0)
您无需坚持要增加确切的变量,只需执行以下操作:
name
返回增加的值series.name = "Series #1"
book_seat()
或 public static int book_seat(boolean [] seats, int current_seat) {
seats[current_seat] = true;
System.out.println(current_seat + 1);
return current_seat + 1;
}
current_first_class_seat
然后,您可以按照先前的意图使用current_eco_seat
进行经济舱和头等舱预订。