我有一个主类,我在其中定义了一个 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;
}