मेरे पास एक हवाई जहाज में बैठने का कार्यक्रम है, लेकिन मुझे नहीं पता कि इसे अभी भी उपलब्ध सीटों की संख्या को प्रिंट करने के लिए कैसे प्राप्त किया जाए और जब मैं q दर्ज करता हूं तो इसे कैसे छोड़ूं। किसी भी तरह की सहायता का स्वागत किया जाएगा। आपका सच में, क्वांग फाम

मुझे यकीन नहीं है कि भरी हुई सीटों की संख्या के लिए काउंट ++ को कहां रखा जाए, और सेंटीनेल क्यू को कैसे सेट किया जाए ताकि प्रोग्राम बंद हो जाए। सीट की व्यवस्था का प्रिंटआउट अच्छा है और एक्स जहां जाना चाहिए वहां जाता है।

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
0
Quang 28 मार्च 2020, 02:04

3 जवाब

आपने बहुत सारे अनावश्यक चर घोषित किए हैं उदा। आपको count की बिल्कुल भी आवश्यकता नहीं है क्योंकि आपके पास पहले से ही एक filled वैरिएबल है जो वही काम कर रहा है। साथ ही, filled को वैश्विक static वैरिएबल के रूप में बनाएं ताकि इसे main और अन्य सभी तरीकों से एक्सेस किया जा सके। वैकल्पिक रूप से, आप इसे विधियों के तर्क के रूप में पारित कर सकते हैं।

संशोधित कार्यक्रम इस प्रकार है:

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

एक नमूना रन:

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.

किसी भी समस्या/संदेह के मामले में टिप्पणी करने के लिए स्वतंत्र महसूस करें।

1
Arvind Kumar Avinash 28 मार्च 2020, 03:37
प्रहरी क्यू के बारे में क्या? क्या कार्यक्रम से बाहर निकलने का कोई तरीका है?
 – 
Quang
28 मार्च 2020, 07:52
कल रात आपको अपना आखिरी संदेश पोस्ट करने के बाद जब मैं बिस्तर पर गया तो मैंने आपका संदेश देखा। ऐसा लगता है कि आपने मेरे द्वारा पोस्ट किया गया प्रोग्राम नहीं चलाया है। बस मेरे उत्तर में पोस्ट किए गए प्रोग्राम को बिना किसी बदलाव के कॉपी और रन करें। मैंने पहले से ही प्रहरी q का ध्यान रखा है और कार्यक्रम भी आवश्यकता के अनुसार बाहर निकल रहा है। किसी भी संदेह/मुद्दे के मामले में टिप्पणी करने के लिए स्वतंत्र महसूस करें।
 – 
Arvind Kumar Avinash
28 मार्च 2020, 15:03
आप सही हे! प्रहरी q एस्केप स्टेटमेंट को दो बार डालने की आवश्यकता है।
 – 
Quang
29 मार्च 2020, 01:59
1
- उत्तर को स्वीकार करना न भूलें ताकि भविष्य के आगंतुक भी आत्मविश्वास से समाधान का उपयोग कर सकें। meta.stackexchange.com/questions देखें /5234/… इसे करने का तरीका जानने के लिए।
 – 
Arvind Kumar Avinash
29 मार्च 2020, 02:06

मैं लाइन डालता हूं int numberOfSeatsAvailable = 28; परिवर्तनीय घोषणा में, और numberOfSeatsउपलब्ध-- ;
System.out.println ("वहाँ हैं" + numberOfSeatsउपलब्ध + " सीट उपलब्ध है।") ; System.out.println (""); इसके बाद जहां सीट लोकेशन लूप में X रखा गया है। मैं अभी भी प्रहरी q पर काम छोड़ रहा हूँ।

-1
Quang 28 मार्च 2020, 03:19

धन्यवाद अरविंद अविनाश! मेरा सही कार्यक्रम दिया गया है:

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
-2
Quang 29 मार्च 2020, 23:07
2
टिप्पणी जोड़ने के लिए उत्तर सुविधा का उपयोग न करें। मैं देखता हूं कि एक अन्य उत्तर ने आपकी समस्या का समाधान करने में आपकी सहायता की। कृपया इसे स्वीकृत के रूप में चिह्नित करें यदि इससे आपकी समस्या हल हो जाती है, जिससे आपकी समस्या का समाधान करने वाले व्यक्ति को उनके प्रयासों के लिए एक योग्य इनाम मिलता है।
 – 
Vince
29 मार्च 2020, 23:11