gpt4 book ai didi

java - 尝试从数组中随机选择一个值时出现错误

转载 作者:行者123 更新时间:2023-12-02 00:09:32 24 4
gpt4 key购买 nike

我有一个主类,其中定义了 Seat 类型的二维数组,然后使用一种方法随机选择一个座位元素并将其设置为“已预订”

但是,如果它必须随机选择 1 个以上(有时即使它只需要随机选择一个)座位元素,它会返回此错误:

"Exception in thread "main" java.lang.NullPointerException
at cinemasystem.Seat.bookSeat(Seat.java:173)
at cinemasystem.CinemaSystem.main(CinemaSystem.java:70)
Java Result: 1"

我不知道我做错了什么,可能是我的随机生成方法的问题,无论如何,我们将不胜感激。

主类:

public static void main(String[] args) {


Seat cinemaactual = new Seat("Cinema");
Seat[][] cinema = new Seat[12][23];
Seat bookedSeat = new Seat("");

Ticket ticket = new Ticket();
Scanner scan = new Scanner(System.in);
String answer, contiune, list;
cinemaactual.CreateTheatre(cinema);
int number, check = 0, category, id;

do {

System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
+ "\nWould you like to purchase tickets or list available seats?"
+ "(/Purchase/List/Help)");
answer = scan.nextLine();

if (answer.equalsIgnoreCase("purchase")) {
do {

System.out.println("Please choose the cateogry of ticket "
+ "you would like, followed by who the ticket is for"
+ "and the amount required. (separated by a space)\n"
+ "1. Gold\n2. Silver\n3. Bronze\n\n1. Adult\n2."
+ " Child\n3. Concession");
category = scan.nextInt();
check = category;
id = scan.nextInt();
number = scan.nextInt();
if (category == 1 || category == 2 || category == 3 && id == 1 || id == 2 || id == 3) {

ticket.SetType(category);
if (category == 1) {
ticket.SetName("Gold");
} else if (category == 2) {
ticket.SetName("Siler");
} else {
ticket.SetName("Bronze");
}
ticket.SetNumber(number);
ticket.SetID(id);
if (id == 1) {
ticket.SetCategory("Adult");
} else if (id == 2) {
ticket.SetCategory("Child");
} else {
ticket.SetCategory("Bronze");
}
System.out.print("You have selected "
+ ticket.GetNumber() + " " + ticket.GetName()
+ " ticket(s) at the " + ticket.GetCategory() + " price.");
System.out.println();
ticket.BuyTicket(category, id);
bookedSeat = Seat.bookSeat(cinema, number);


} else {

System.out.print("Sorry, incorrect input, please enter an apropriate value.");
check = scan.nextInt();
}
} while (check == 0 || check > 3);

do {
System.out.print("Would you like to perchase more tickets? (Yes/No)");
contiune = scan.nextLine();


if (contiune.equalsIgnoreCase("Yes")) {
System.out.print("Would you like to list available seats? (Yes/No) (A/G/S/B)");
list = scan.nextLine();
cinemaactual.DisplayTheatre(cinema);
if (list.equalsIgnoreCase("Yes")) {
cinemaactual.DisplayTheatre(cinema);

}
}

随机选择座位的方法,在我的座位类别中:

 public static Seat bookSeat(Seat[][] x, int number){
int count = 0;
Seat book = new Seat("");
Random rand = new Random();
while(count < number){
if (x != null) {

do {
book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];
book.bookSeat(true);}

while (book.isBooked());
} count++; }
return book;
}

座位等级第 172 和 173 行

172: book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];

173: book.bookSeat(true);}

Seat 类中的 CreateTheatre 方法:

 public Seat[][] CreateTheatre(Seat[][] x) {

for (int row = 0; row < 8; row++) {
for (int col = 0; col < 4; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 8; row < 12; row++) {
for (int col = 0; col < 4; col++) {
x[row][col] = new Seat("S");
}
}

for (int row = 0; row < 8; row++) {
for (int col = 19; col < 23; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 8; row < 12; row++) {
for (int col = 19; col < 23; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 4; col < 9; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 14; col < 19; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 9; row < 12; row++) {
for (int col = 7; col < 4; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 14; col < 20; col++) {
x[row][col] = new Seat("B");
}
}

for (int row = 5; row < 9; row++) {
for (int col = 4; col < 9; col++) {
x[row][col] = new Seat("S");
}
}

for (int row = 5; row < 9; row++) {
for (int col = 14; col < 20; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 6; row < 9; row++) {
for (int col = 9; col < 14; col++) {
x[row][col] = new Seat("G");
}
}
for (int row = 9; row < 12; row++) {
for (int col = 7; col < 16; col++) {
x[row][col] = new Seat("G");
}
}

for (int row = 9; row < 12; row++){
for (int col = 4; col < 7; col++){
x[row][col] = new Seat("S");
}
}

for (int row = 9; row < 12; row++){
for (int col = 16; col < 19; col++){
x[row][col] = new Seat("S");
}
}
return x;
}

最佳答案

如果您检查了CreateTheatre逻辑,cinema内的值可能为空。通过一个循环将默认值放入数组中。

以行 0 为例,您只需填充从 04 的值,然后将 19 填充到 23。除此之外,我没有看到您填写值。

for(int i = 0; i<cinema.length;i++)
for(int j=0; j<cinema[i].length;j++)
cinema[i][j] = new Seat("");

更新:如果不想放置空座位,那么您可以在调用 book.bookSeat(true)

之前添加 null 检查
if(book != null){
book.bookSeat(true);
}
<小时/>

您可以使用Arrays.deepToString(cinema)打印多维数组。

正如其他线程使用中所指出的book = x[rand.nextInt(12)][rand.nextInt(23)]

关于java - 尝试从数组中随机选择一个值时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13127102/

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