gpt4 book ai didi

java - 如何在 java 的航空公司中随机安排一组乘客?一步步

转载 作者:行者123 更新时间:2023-12-04 20:16:20 25 4
gpt4 key购买 nike

我是 Java 的新手,我正在开发一个关于航空公司乘客座位组的程序。 (我会尽量让我的问题简单易懂)

所以,我应该让航空公司的乘客分组。我得到有关他们如何按预订舱位就座的信息。

public class Bookings {
public Bookings() {
}

public static void main(String[] var0) {
System.out.println("Do not run this program.");
System.out.println("Call the method as follows:");
System.out.println(" String[] bookings = Bookings.getBookings()");
}

public static String[] getBookings() {
return new String[]{"38", "2", "Gurganus, Erik", "Gurganus, Fernando", "1", "Cahn, Lance", "1", "Burrough, Jamie", "3", "Riney, Christian", "Marceau, Hillary", "Marceau, Julio", "3", "Gariepy, Noemi", "Gariepy, Louisa", "Gariepy, Nelson", "2", "Mazzoni, Max", "Fiorita, Tyrone", "3", "Ehle, Clinton", "Minnifield, Clinton", "Blinn, Jamie", "2", "Sokolowski, Kurt", "Sokolowski, Sofia", "2", "Secord, Hugh", "McVeigh, Karina", "1", "McMonagle, Christian", "1", "Canchola, Clayton", "2", "Duer, Julio", "Danos, Ted", "3", "Regal, Christian", "Mun, Allan", "Mun, Lakisha", "2", "Noblitt, Karina", "Tussey, Clayton", "1", "Seckman, Jamie", "2", "Folmar, Edwina", "Lokey, Clayton", "2", "Pippen, Javier", "Saba, Earlene", "4", "Tippit, Lorrie", "Tippit, Harriett", "Tippit, Clare", "Tippit, Lance", "3", "Mazurek, Mallory", "Mazurek, Stefan", "Mazurek, Ihor", "2", "Saini, Amie", "Peavler, Darcy"};
}
}

根据这些信息,我创建了一个名为 seats 的字符串数组和一个调用该类以检索其中所有信息的预订数组

String[] seats = new String[38];
String[] bookings = Bookings.getBookings();

第一个值表示我的航类可用的座位数,其余值表示要分配的组数及其名称。

为了进一步说明,此过程要遵循的步骤是:

Your program will seat passengers as follows: It will create a "seats" array of the appropriate size (given in the first >position of the "bookings" array). DONE

It will process the remaining items in the "bookings" array of strings. >For each group of passengers it will attempt to seat them as follows: DONE

  • First, it will see if there are enough seats remaining on the flight for everyone in the group; if not, it will display an error message and not assign seats to anyone in the group.
  • Secondly, it will go through the "seats" array to determine if a block of empty seats large enough to seat the entire group together is available (for example, if the group size is 3, it will see if there are 3 consecutive empty seats).

    If there is at least one such block of seats anywhere in the array, randomly assign the group to one of these blocks by randomly selecting an element in the "seats" array.

    If that seat is empty, determine if enough successive array elements (seats) are also empty to seat the entire group; if so, seat them there.

    Otherwise, randomly try another seat, repeating until successful.

    If there is no such block, randomly assign each passenger in the group individually to a seat (i.e., the group will be split up). For each passenger, pick random seat numbers until you find an empty seat.

我现在的问题是这两个步骤,我把这一步写在一张纸上来解决它,但我不确定我是否在正确的轨道上。任何贡献/帮助将不胜感激。如果需要,我可以提供有关该问题的更多信息。

for(int i =0; i<seats.length; i++) {
if(seats[i] == null) {
for(int i =0; i<seats.length; i++) {
if(seats.equals(null) > passengerGroup) {
(randomly seat them consecutively);
} else {
(randomly seat them anywhere)
}
}
} else {
system.out.println( "No seats available");
}
}

最佳答案

在写循环之前分解问题;在这种情况下,计数器是跟踪可用性的最简单方法。

public static void main(String[] args) {

int numSeats = 20;
int available = numSeats;
int numGroups = 6;

int groupSizes[] = new int[numGroups];

groupSizes[0] = 2;
groupSizes[1] = 1;
groupSizes[2] = 5;
groupSizes[3] = 3;
groupSizes[4] = 10;
groupSizes[5] = 2;

for (int i=0; i < groupSizes.length; i++) {

if (available > groupSizes[i]){
available -= groupSizes[i];
System.out.println("Group: " + i + " has been book on!");
}

else {
System.out.println("Not enough seats for group: " + i);
}

}
System.out.println(available + " Seats remaining");
}

假设这是家庭作业,这可能有点帮助,但它仍然需要一些工作,所以我希望它能让你走上正确的道路。编辑:实际上重新阅读问题,这可能与您想要的有点不同,所以希望它能让您了解如何开始。

关于java - 如何在 java 的航空公司中随机安排一组乘客?一步步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33027899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com