简体   繁体   English

用于剧院的Java 2D座位阵列

[英]Java 2D Seating Array for a Theater

import java.util.*;

/**
 * Write a description of class TheaterApp here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TheaterApp {

    static int [][] seats = {  
            {10,10,10,10,10,10,10,10},
            {10,10,10,10,10,10,10,10},
            {10,10,20,20,20,20,10,10},
            {20,20,30,30,30,30,20,20},
            {30,30,40,40,40,40,30,30},
            {30,40,40,50,50,40,40,40}};

    /**
     * Constructor for objects of class TheaterApp
     */
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        String ans;

        do {
            System.out.print("Enter request, please (or 'help' or 'quit') ");
            ans = input.next();

            if (ans.equals("help")) {
                System.out.println("Possible commands");
                System.out.println("price <price>");
                System.out.println("seat <row> <seat>");
                System.out.println("left");
                System.out.println("remaining <price>");
                System.out.println("print");
                System.out.println("quit");
                System.out.println("help");

            } else if (ans.equals("price")) {
                int p = input.nextInt();

                for (int i = 0; i < seats.length; i++) {
                    for (int j = 0; j < seats[i].length; j++) {
                        if (seats[i][j] == 0) {
                            System.out.println("Next available seat at position: " + i + " " + j);
                        }
                    }
                }

                // Find the 'best' seat at the given price
            } else if (ans.equals("seat")) {
                int r = input.nextInt();
                int c = input.nextInt();
                int k;
                int i;
                int j;
                for (int l = 0; l < 6; l++) {

                    k = 1;
                    for(i=0;i<6;i++) {
                        for(j=0;j<8;j++) {
                            if (k == input.nextInt()) {
                                // check if the seat has already been reserved
                                if (seats[i][j]== 0) {
                                    System.out.println("That seat has already been reserved");
                                }
                                // if its not reserved then reserve it
                                else {
                                    seats[i][j]= 0;
                                }
                            }
                            k++;

                            System.out.println(seats[i][j]);
                        }
                    }
                }

                // Reserve the given row and seat, if possible
            } else if (ans.equals("left")) {


                // Print the total available seats


            } else if (ans.equals("remaining")) {
                int p = input.nextInt();


                // Print the total available seats at this price


            } else if (ans.equals("print")) {
                for (int r = 0; r < seats.length; ++r) {
                    for (int s = 0; s < seats[r].length; ++s) {
                        System.out.print(seats[r][s] + " ");
                    }
                    System.out.println();
                }
            } else if (!ans.equals("quit")) {
                System.out.println("Come again?");
            }
        } while (!ans.equals("quit"));
        System.out.println("Good bye");
    }
}

This array represents theater seats and I have to mark sold seats by changing the price to 0. I also have to make sure seats are open when a user asks for aa certain spot, and when a user enters a price, find any seats that are open. 这个数组代表剧院座位,我必须通过将价格更改为0来标记已售出的座位。我还必须确保当用户要求某个位置时座位是打开的,并且当用户输入价格时,找到所有打开。

So I'm pretty sure I figured out the code for finding the best seat at any given price. 因此,我很确定自己能找到找到以给定价格找到最佳座位的代码。 I can't figure out how to do the remaining code. 我不知道如何做其余的代码。

I just need to find out how to print the total available seats and also how to print the total available seats when a certain price is entered. 我只需要了解输入特定价格后如何打印可用总座位数以及如何打印可用总座位数。

Thanks. 谢谢。

You'd just use nested for loops, like you did before, except now you'd have some kind of a availableSeats counter you'll increment every time a seat meets a certain condition. 您将像以前一样只使用嵌套的for循环,除了现在有了某种availableSeats Seats计数器,每当座位满足特定条件时,该计数器都会增加。

Like so: 像这样:

    int availableSeats = 0;
    for(i=0;i<6;i++) {
        for(j=0;j<8;j++) {
            if(seats[i][j] == sometargetprice){
                availableSeats++;
            }
        }
    }
    System.out.println("Total of " + availableSeats + " are available.");

Unless I'm not understanding the problem correctly. 除非我不能正确理解问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM