gpt4 book ai didi

java - 二维数组帮助如何匹配和转换为字符串

转载 作者:行者123 更新时间:2023-12-01 13:44:37 24 4
gpt4 key购买 nike

我必须编写这个类来记录飞机的座位安排,我的两种方法遇到了问题,方法说:

  • makeReservation(id, request) — 此方法直接访问座位以确定座位是否已被占用。如果没有,则记录席位中该位置的 id。如果您进行了预订,则返回 true;如果已预订,则返回 false,
  • getAssignment(id) — 按行和列搜索,直到在席位中找到匹配的 ID,然后将行和列转换为字符串(例如 seats[5][2] == id 应返回“6C”。

我该怎么做?这是我到目前为止的代码:

import java.util.ArrayList;

class Plane {
private int[][] seats;
String flight;
String departure;

public Plane(String name, String leaving, int length, int width) {
flight = name;
departure = leaving;
for (length = 0; length < 22; length++)
for (width = 0; width < 5; width++)
seats[length][width] = -1;
}

public String getFlight() {
return flight;
}

public String getDepartureTime() {
return departure;
}

public void setDepartureTime(String newTime) {
this.departure = newTime;
}

public boolean makeReservation(int id, AvilaNatalyPassenger request) {
boolean status = true;
ArrayList<Integer> inputValues = new ArrayList<Integer>();
for (int i = 0; i < 22; i++) {
for (int j = 0; j < 5; j++) {
id = seats[i][j];
if (id != -1) {
if (inputValues.contains(id)) {
status = false;
break;
} else {
inputValues.add(id);
// for(int x = 0; x < inputValues.size; x++)
// for(int y = 0; y < inputValues[x].size; y++)
// inputValues[x][y] = seats[x][y];

}
}
}
}

return status;
}

public int getId(int row, int seat) {
if (row >= 0 && row < seats[0].length && seat >= 0
&& seat < seats.length) {
return seats[row][seat];
} else {
return -1;
}
}

public String getAssignment(int id){
for (



}
}

最佳答案

声明一个带有字母的静态数组

private static String[] ALPHABET = new String[]{"A", "B", "C", ..., "Z"};

...

public String getAssignment(int id)
{

for (int i = 0; i < 22; i++)
for (int j = 0; j < 5; j++)
if (seats[i][j] == -1) return new String((j+1) + ALPHABET[i]);

}

关于java - 二维数组帮助如何匹配和转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20445317/

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