1

Java ヘルプ。

座席表を画面に出力し、ユーザーに座席か料金のどちらかを選択させるプログラムを作成してください。価格を 0 (ゼロ) に変更して、販売済みシートをマークします。ユーザーが座席を指定するときは、空席があることを確認してください。利用できない場合は、ユーザーに通知し、座席または価格のいずれかを選択するよう促します。

先生は、行が AI で、列が 1 ~ 10 であることを望んでいますが、リベラル アーツ スクールでこの高度な Java を学ばなかったので、これを取得できません。先生も出来ないステージを出力して欲しい。

public class TheatreSeating {
    public static void main(String[] args) { 


        // Two dimensional array to hold seats
        int[10][9] seatsArray = 
                 {{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
                { 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
                { 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
                { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
                { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
                { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
                { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
                { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
                { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }};
       String continueFlag = "Y";
        Scanner input = new Scanner(System.in);
        while (continueFlag.equals("Y") || continueFlag.equals("y")) {
            System.out.println("Please Select an option");
            System.out.println("1. Select Seat");
            System.out.println("2. Select Price");

            // Get selected option
            int option = input.nextInt();
            if (option == 1) {
                System.out
                        .println("Please enter the row number of seat (1-10):");
                // Subtract 1 as array starts from 0
                char row = input.nextInt() - 1;
                System.out
                        .println("Please enter the column number of seat (1-10):");
                // Subtract 1 as array starts from 0
                int column = input.nextInt() - 1;
                boolean seatAvailable = isSeatAvailable(seatsArray, row, column);
                if (seatAvailable) {
                    // Assign Seat
                    seatsArray[row][column] = 0;
                } else {
                    System.out.println("Requested seat is not available");
                }
            } else if (option == 2) {
                System.out
                        .println("Please enter the Price(10,20,30,40 or 50):");
                int price = input.nextInt();
                // Check available seat
                boolean found = false;
                for (int i = 0; i < seatsArray.length; i++) {
                    // Continue looking only if not found
                    if (found == false) {
                        for (int j = 0; j < seatsArray[i].length; j++) {
                            if (seatsArray[i][j] == price) {
                                // Assign the seat
                                seatsArray[i][j] = 0;
                                found = true;
                                // Exit from inner loop
                                break;
                            }
                        }
                    }
                }
            }
            printSeats(seatsArray);
            System.out.println("Do you want to enter more seats (Y/N)");
            continueFlag = input.next();
        }
        System.out.println("Bye");
    }

    private static void printSeats(int[][] seatsArray) {
        for (int i = 0; i < seatsArray.length; i++) {
            for (int j = 0; j < seatsArray[i].length; j++) {
                System.out.print(seatsArray[i][j] + " ");
            }
            System.out.println();
        }
    }

    private static boolean isSeatAvailable(int[][] seatsArray, int row,
            int column) {
        for (int i = 0; i < seatsArray.length; i++) {
            for (int j = 0; j < seatsArray[i].length; j++) {
                if (i == row && j == column && seatsArray[i][j] != 0) {
                    return true;
                }
            }
        }
        return false;
    }
}
4

1 に答える 1