简体   繁体   English

飞机座位程序(二维数组)

[英]Airplane seating program (2 dimensional array)

I have an airplane seating program but I don't know how to get it to print the number of seats still available and how to get it to quit when I enter q.我有一个飞机座位程序,但我不知道如何让它打印仍然可用的座位数量以及如何在我输入 q 时让它退出。 Any help would be greatly appreciated.任何帮助将不胜感激。 Yours truly, Quang Pham您真诚的,广范

I'm not certain where to place the count++ to number the filled seats, and how to set the sentinel q so the program quits.我不确定在哪里放置 count++ 来为已填充的座位编号,以及如何设置哨兵 q 以便程序退出。 The printout of the seat arrangement is good and the X goes where it should.座位安排的打印输出很好,X 去到了它应该去的地方。

import java.util.Scanner ;
/**
 * The AirplaneSeating program asks the user for the seat they would like to reserve.
 * A layout of the plane is printed and an X is placed in the reserved seat.  The 
 * program finds if the seat is available and if the entry is valid.  A sentinel of q
 * ends the program.
 *
 * @author Quang Pham
 * @version Module8, Lab 2, 4/1/20
 * 
 *    Algorithm:
 *    
 *    1. Greet user and ask which seat they would like to reserve.
 *    2. Print a layout of the plane and the seats available. 
 *    3. Put an X in the position where the user would like to reserve.
 *    4. Loop and ask if they'd like to reserve another seat.
 *    5. Make certain seat is available and the entry is valid, if sentinel -1
 *       is entered, exit program.
 *    
 *    Problem Description:
 *    
 *    Write a program to assign passenger's seats in a small airplane.  Assume the 
 *    plane has its seats numbered as follows:
 *
 *   Row
 *    1   A B  C D  
 *    2   A B  C D
 *    3   A B  C D
 *    4   A B  C D
 *    5   A B  C D
 *    6   A B  C D
 *    7   A B  C D
 *
 *          You should verify that the user enters rows between 1 and 7 only, and
 *     columns A, B, C, or D only.  If the user enters an entry that is invalid,
 *     print an error message telling them what's wrong, then prompt for the next
 *     entry.  Model the seats in the plane using a multi-dimensional array with
 *     seven rows and four columns.  Use a loop in your program which continues to
 *     prompt for a seat to reserve until either the user specifies a sentinel to
 *     stop the program, or when all seats are reserved.
 *          After each entry from the user, the program should display the seat
 *     reservation pattern, with an 'X' marking the seats already assigned. For 
 *     example, after seats 1A, 2B, and 4C are reserved, the display might show 
 *     the following:
 *
 *  Row
 *    1   X B  C D
 *    2   A X  C D
 *    3   A B  C D
 *    4   A B  X D
 *    5   A B  C D
 *    6   A B  C D
 *    7   A B  C D
 *
 *       There are 25 seats available.  This continues until either all seats are
 *  filled or the user enters a sentinel indicating that he/she is done entering
 *  reservations.  If the user tries to reserve a seat which is already taken, the
 *  program should say that that seat is occupied and ask for another choice.
 *       Submit program files for all classes, as well as a print screen or screen
 *  snip showing what your screen looks like after 4 or 5 seats have been assigned.
 *  Be sure to demonstrate what happens when the user tries to reserve a seat that
 *  is already taken or specifies an invalid seat (for example, 9A or 5E).
 */
public class AirplaneSeating
{
    int count = 0 ;
    public static void main(String[] args)
    {
        // two- dimensional array with 7 rows and 4 columns 
        char[][] seats = new char [7][4] ;
        for (int i = 0; i < 7; i++)
        {
            seats[i][0] = 'A' ;
            seats[i][1] = 'B' ;
            seats[i][2] = 'C' ;
            seats[i][3] = 'D' ;
        }

        String seatNumber = " " ; 
        int count = 0 ;
        String q = " " ;
        int numberOfSeatsAvailable = 0 ;
        int filled = 0 ;
        System.out.println("Welcome to the Airplane Seating Reservation System.") ;
        System.out.println("Please enter the seat (e.g.- 1A) you wish to reserve.") ;
        System.out.println("Enter q to exit.") ;
        Scanner keyboard = new Scanner(System.in) ;
        seatNumber = keyboard.nextLine() ;
        count++ ;
        if (seatNumber.equals("q"))
       {
            System.out.println("Program ended.") ;
            System.exit(0) ;
       } 
        else
       {
         //print seating pattern and put an X in the seatNumber location
         while((filled < 28) && (seatNumber.length() > 0))
         {
            int row = seatNumber.charAt(0) - '1' ;
            int col = seatNumber.charAt(1) - 'A' ;
            count ++ ;
            if (row < 0 || row > 7 || col < 0 || col > 4)
            {
                System.out.println("Input error. Enter seat to assign (e.g., '1A')," +
                    "or q to quit.");
                seatNumber = keyboard.nextLine() ;
                count++ ;
            }
            else
            {
                if (seats[row][col] != 'X')
                {
                    seats[row][col] = 'X' ;
                    filled++;
                    System.out.println(" ") ;
                    printSeats(seats);
                }
                if (filled < 28)
                {
                    System.out.println("Enter seat to assign (e.g., '1A')," +
                        "or q to quit.");
                    seatNumber = keyboard.nextLine();
                    count++ ;
                }
             }
          }         
        }
    }

    private static void printSeats(char[][] seats)
    {
        int count = 0;
        System.out.println("Row") ;
        for (int i = 0; i < seats.length; i++)
        {
            System.out.println((i + 1) + "  " + 
                seats[i][0] + " " + seats[i][1] + "  " + seats[i][2] + " " + seats[i][3]) ;

        }
        count++ ;
        int numberOfSeatsAvailable = 0 ;     
        numberOfSeatsAvailable = (28 - count) ;      
        System.out.println("There are " + numberOfSeatsAvailable + " seats available.") ;
    }  //end main
}  //end class

You have declared so many unnecessary variables eg you do not need count at all since you already have a filled variable doing the same thing.你已经声明了这么多不必要的变量,例如你根本不需要count因为你已经有一个filled变量做同样的事情。 Also, make filled as a global static variable so that it can be accessed in main and all other methods.此外,将 make filled为全局static变量,以便可以在main和所有其他方法中访问它。 Alternatively, you can pass it as an argument to the methods.或者,您可以将其作为参数传递给方法。

The corrected program is as follows:修正后的程序如下:

import java.util.Scanner;

public class AirplaneSeating {

    static int filled = 0;

    public static void main(String[] args) {
        // two- dimensional array with 7 rows and 4 columns
        char[][] seats = new char[7][4];
        for (int i = 0; i < 7; i++) {
            seats[i][0] = 'A';
            seats[i][1] = 'B';
            seats[i][2] = 'C';
            seats[i][3] = 'D';
        }

        String seatNumber = " ";
        String q = " ";
        System.out.println("Welcome to the Airplane Seating Reservation System.");
        System.out.println("Please enter the seat (e.g.- 1A) you wish to reserve.");
        System.out.println("Enter q to exit.");
        Scanner keyboard = new Scanner(System.in);
        seatNumber = keyboard.nextLine();
        if (seatNumber.equals("q")) {
            System.out.println("Program ended.");
            System.exit(0);
        }
        // print seating pattern and put an X in the seatNumber location
        while (filled < 28 && seatNumber.length() > 0) {
            int row = seatNumber.charAt(0) - '1';
            int col = seatNumber.charAt(1) - 'A';
            if (row < 0 || row > 7 || col < 0 || col > 4) {
                System.out.println("Input error. Enter seat to assign (e.g., '1A')," + "or q to quit.");
                seatNumber = keyboard.nextLine();
                if (seatNumber.equals("q")) {
                    System.out.println("Program ended.");
                    System.exit(0);
                }
            } else {
                if (seats[row][col] != 'X') {
                    seats[row][col] = 'X';
                    filled++;
                    System.out.println(" ");
                    printSeats(seats);
                }
                if (filled < 28) {
                    System.out.println("Enter seat to assign (e.g., '1A')," + "or q to quit.");
                    seatNumber = keyboard.nextLine();
                    if (seatNumber.equals("q")) {
                        System.out.println("Program ended.");
                        System.exit(0);
                    }
                }
            }
        }
    }

    private static void printSeats(char[][] seats) {
        System.out.println("Row");
        for (int i = 0; i < seats.length; i++) {
            System.out
                    .println((i + 1) + "  " + seats[i][0] + " " + seats[i][1] + "  " + seats[i][2] + " " + seats[i][3]);

        }
        int numberOfSeatsAvailable = (28 - filled);
        System.out.println("There are " + numberOfSeatsAvailable + " seats available.");
    } // end main
} // end class

A sample run:示例运行:

Welcome to the Airplane Seating Reservation System.
Please enter the seat (e.g.- 1A) you wish to reserve.
Enter q to exit.
3B

Row
1  A B  C D
2  A B  C D
3  A X  C D
4  A B  C D
5  A B  C D
6  A B  C D
7  A B  C D
There are 27 seats available.
Enter seat to assign (e.g., '1A'),or q to quit.
6A

Row
1  A B  C D
2  A B  C D
3  A X  C D
4  A B  C D
5  A B  C D
6  X B  C D
7  A B  C D
There are 26 seats available.
Enter seat to assign (e.g., '1A'),or q to quit.
4C

Row
1  A B  C D
2  A B  C D
3  A X  C D
4  A B  X D
5  A B  C D
6  X B  C D
7  A B  C D
There are 25 seats available.
Enter seat to assign (e.g., '1A'),or q to quit.
q
Program ended.

Feel free to comment in case of any issue/doubt.如有任何问题/疑问,请随时发表评论。

I place the line int numberOfSeatsAvailable = 28 ;我将行 int numberOfSeatsAvailable = 28 ; in the variable declaration, and numberOfSeatsAvailable-- ;在变量声明中,和 numberOfSeatsAvailable-- ;
System.out.println("There are " + numberOfSeatsAvailable + " seats available.") ; System.out.println("有" + numberOfSeatsAvailable + " 个座位。") ; System.out.println(" ") ; System.out.println(" ") ; in after where an X is placed in the seat location loop.在 X 放置在座椅位置循环中的位置之后。 I'm still working on the sentinel q for quit part.我仍在研究退出部分的哨兵 q。

Thank you Arvind Avinash!谢谢阿文德·阿维纳什! My corrected program is given:给出了我更正的程序:

import java.util.Scanner ;
/**
 * The AirplaneSeating program asks the user for the seat they would like to reserve.
 * A layout of the plane is printed and an X is placed in the reserved seat.  The 
 * program finds if the seat is available and if the entry is valid.  A sentinel of q
 * ends the program.
 *
 * @author Quang Pham
 * @version Module8, Lab 2, 4/1/20
 * 
 *    Algorithm:
 *    
 *    1. Greet user and ask which seat they would like to reserve.
 *    2. Print a layout of the plane and the seats available. 
 *    3. Put an X in the position where the user would like to reserve.
 *    4. Loop and ask if they'd like to reserve another seat.
 *    5. Make certain seat is available and the entry is valid, if sentinel q
 *       is entered, exit program.
 *    
 *    Problem Description:
 *    
 *    Write a program to assign passenger's seats in a small airplane.  Assume the 
 *    plane has its seats numbered as follows:
 *
 *   Row
 *    1   A B  C D  
 *    2   A B  C D
 *    3   A B  C D
 *    4   A B  C D
 *    5   A B  C D
 *    6   A B  C D
 *    7   A B  C D
 *
 *          You should verify that the user enters rows between 1 and 7 only, and
 *     columns A, B, C, or D only.  If the user enters an entry that is invalid,
 *     print an error message telling them what's wrong, then prompt for the next
 *     entry.  Model the seats in the plane using a multi-dimensional array with
 *     seven rows and four columns.  Use a loop in your program which continues to
 *     prompt for a seat to reserve until either the user specifies a sentinel to
 *     stop the program, or when all seats are reserved.
 *          After each entry from the user, the program should display the seat
 *     reservation pattern, with an 'X' marking the seats already assigned. For 
 *     example, after seats 1A, 2B, and 4C are reserved, the display might show 
 *     the following:
 *
 *  Row
 *    1   X B  C D
 *    2   A X  C D
 *    3   A B  C D
 *    4   A B  X D
 *    5   A B  C D
 *    6   A B  C D
 *    7   A B  C D
 *
 *       There are 25 seats available.  This continues until either all seats are
 *  filled or the user enters a sentinel indicating that he/she is done entering
 *  reservations.  If the user tries to reserve a seat which is already taken, the
 *  program should say that that seat is occupied and ask for another choice.
 *       Submit program files for all classes, as well as a print screen or screen
 *  snip showing what your screen looks like after 4 or 5 seats have been assigned.
 *  Be sure to demonstrate what happens when the user tries to reserve a seat that
 *  is already taken or specifies an invalid seat (for example, 9A or 5E).
 */
public class AirplaneSeating
{

    static int filled = 0 ;

    public static void main(String[] args)
    {

        // two- dimensional array with 7 rows and 4 columns 
        char[][] seats = new char [7][4] ;
        for (int i = 0; i < 7; i++)
        {
            seats[i][0] = 'A' ;
            seats[i][1] = 'B' ;
            seats[i][2] = 'C' ;
            seats[i][3] = 'D' ;
        }

        String seatNumber = " " ; 
        String q = " " ;
        int numberOfSeatsAvailable = 28 ;
        System.out.println("Welcome to the Airplane Seating Reservation System.") ;
        System.out.println("Please enter the seat (e.g.- 1A) you wish to reserve.") ;
        System.out.println("Enter q to exit.") ;
        printSeats(seats) ;
        Scanner keyboard = new Scanner(System.in) ;
        seatNumber = keyboard.nextLine() ;
        if (seatNumber.equals("q"))
                {
                    System.out.println("Program ended.") ;
                    System.exit(0) ;
                } 
        while((filled < 28) && (seatNumber.length() > 0))
        {
            int row = seatNumber.charAt(0) - '1' ;
            int col = seatNumber.charAt(1) - 'A';
            if (row < 0 || row > 7 || col < 0 || col > 4)
            {
                System.out.println("Input error. Enter seat to assign (e.g., '1A')," +
                    "or q to quit.");
                seatNumber = keyboard.nextLine() ;
                if (seatNumber.equals("q"))
                {
                    System.out.println("Program ended.") ;
                    System.exit(0) ;
                } 
            }
            else
            {
                //put an X in the assigned seat and print seating arrangement
                if (seats[row][col] != 'X')
                {
                    seats[row][col] = 'X' ;
                    filled++ ; 
                    //System.out.println(" ") ;
                    printSeats(seats) ;
                }
                if (filled < 28)
                {
                    System.out.println("Enter seat to assign (e.g., '1A')," +
                        "or q to quit.");
                    seatNumber = keyboard.nextLine();
                    if (seatNumber.equals("q"))
                    {
                        System.out.println("Program ended.") ;
                        System.exit(0) ;
                    } 
                }
            }
        }       
        System.out.println("Final seat assignments: "); 
        printSeats(seats); 
    }

    private static void printSeats(char[][] seats)
    {
        System.out.println("Row") ;
        for (int i = 0; i < seats.length; i++)
        {
            System.out.println((i + 1) + "  " + 
                seats[i][0] + " " + seats[i][1] + "  " + seats[i][2] + " " + seats[i][3]) ;
        }
        int numberOfSeatsAvailable = (28 - filled);
        System.out.println("There are " + numberOfSeatsAvailable + " seats available.");
        System.out.println(" ");
    }  //end main
}  //end class

Can someone please make the same code in jupyter notebook.有人可以在 jupyter notebook 中制作相同的代码吗? Python code PLease!!! Python代码请!!!

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

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