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

时间:2016-10-14 03:06:55

标签: java arrays input prompt

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来标记已售出的座位。我还必须确保当用户要求某个地点时座位是打开的,当用户输入价格时,找到任何座位开放的座位。

所以我很确定我找到了以任何给定价格找到最佳座位的代码。我无法弄清楚如何处理剩余的代码。

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

感谢。

1 个答案:

答案 0 :(得分:0)

您只需使用嵌套的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.");

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