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

时间:2012-10-28 05:43:29

标签: java arrays methods 2d

所以基本上我想用对象填充数组然后显示它。 (稍后这些对象将能够成为'预订',这将使它们在数组中显示为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"));


    }

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

    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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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