【问题标题】:Java 2D Seating Array for a Theater剧院的 Java 2D 座位阵列
【发布时间】:2024-08-15 13:40:02
【问题描述】:
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");
    }
}

这个数组代表剧院座位,我必须通过将价格更改为 0 来标记已售出的座位。我还必须确保当用户要求某个位置时座位是开放的,并且当用户输入价格时,找到任何开放的座位。

所以我很确定我找到了以任何给定价格找到最佳座位的代码。我不知道怎么做剩下的代码。

我只需要了解如何打印可用座位总数,以及在输入某个价格时如何打印可用座位总数。

谢谢。

【问题讨论】:

    标签: java arrays input prompt


    【解决方案1】:

    您只需使用嵌套的 for 循环,就像您之前所做的那样,但现在您将拥有某种 availableSeats 计数器,您将在每次座位满足特定条件时递增。

    像这样:

        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.");
    

    除非我没有正确理解问题。

    【讨论】:

    • 感谢这帮助我解决了这个问题。我只需将 6 和 8 分别更改为 5 和 7 以适应数组。欣赏它。
    • 没关系 6 和 8 是对的,我的错……再次感谢。
    • 很高兴帮助 :] 顺便说一句,由于两者的代码几乎相同,我建议将循环写入一个单独的函数,将 targetPrice 传递给它,并让它返回 availableSeats到底。这使代码看起来更简洁,并且更易于维护。