gpt4 book ai didi

java - 为什么我的数组没有填充对象/显示?

转载 作者:行者123 更新时间:2023-12-01 15:04:55 24 4
gpt4 key购买 nike

所以基本上我想用对象填充一个数组,然后显示它。 (稍后这些对象将能够“预订”座位,这将使它们在数组中显示为 X。)

但是,每当我执行创建和显示数组的方法时,我都会收到错误。

这是我的代码:

主要:

public static void main(String[] args) {


Seat cinemaactual = new Seat("Cinema");
Seat[][] cinema = new Seat[10][22];

Ticket ticket = new Ticket();
Scanner scan = new Scanner(System.in);
String answer, contiune;



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();
number = scan.nextInt();
id = 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 .");

ticket.BuyTicket(category, id, 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();




} while (contiune.equalsIgnoreCase("Yes"));



} else if (answer.equalsIgnoreCase("list")) {
cinemaactual.CreateTheatre(cinema);
cinemaactual.DisplayTheatre(cinema);

} else if (answer.equalsIgnoreCase("help")) {
// Code for help
} else if (answer.equalsIgnoreCase("quit")) {
System.exit(-1);
}

System.out.print("Sorry, incorrect input please enter"
+ " a valid input (Purchase/List/Help or QUIT to exit");
answer = scan.nextLine();

} while (!answer.equalsIgnoreCase("purchase")
|| !answer.equalsIgnoreCase("List")
|| !answer.equalsIgnoreCase("help")
|| !answer.equalsIgnoreCase("quit"));


}

创建/填充数组的 Seat 类方法:

    public void SetType(String x) {

type = x;
}

public boolean SetStatus() {
return status;
}

public void GetStatus(boolean x) {
status = x;
}

public String toString() {
String Seat = type;

return type;
}

public String BookSeat() {
type = "x";
return type;
}

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


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

public void DisplayTheatre(Seat[][] x) {
for (int row = 0; row < x.length; row++) {
for (int col = 0; col < x[row].length; col++) {
System.out.print(x[row][col]);
}
System.out.println();
}
}
}

最佳答案

CreateTheatre(...) 中,您首先必须确保数组实际上与您在 for 循环中处理的一样大(如果它们是)较小,尝试访问它们会抛出 ArrayIndexOutOfBoundsException

关于java - 为什么我的数组没有填充对象/显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13106678/

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