-4

我正在尝试创建一个名为 isAvailable 的布尔方法,它接受航班上的座位号作为参数,返回一个指示该座位是否可用的布尔值。使用类似于井字游戏程序的代码,该代码会将座位号(如 20)转换为二维数组的行和列索引。如果航班上有任何可用座位,则返回 true。

    public class Flight {

     /*
     * declare instance variables
     * each flight object that is created will have its own copy of these
     * These are declared as private to ensure that external Java code
     * cannot access them directly and assign any arbitrary values to them.
     */
     String flightNumber;
     int rows;
     int seatsPerRow;
     boolean [][] seat;
     double fare;
     String origin;
     String destination;


     /*
     * The constructor
     * Contains code that initially sets up each flight object when it is
     * first created. Always has the same name as the class.
     */
     public Flight (String flightNumber, int rows, int seatsPerRow, double fare,  String origin, String destination) {

    //call the mutator methods and let them initialize the instance variables
    setflightNumber(flightNumber);
    setRows(rows);
    setSeatsPerRow(seatsPerRow);
    setFare(fare);
    setOrigin(origin);
    setDestination(destination);

    seat = new boolean [rows][seatsPerRow]

    for(int i = rows, int x = seatsPerRow; i>0, x>0; i--, x--){
        seat[i][x] = true;
    }
     }//end of constructor


         /*
         * The accessor methods
     * These report the current values of the Flight object's instance variables
     */
     public String getFlightNumber() {
         return flightNumber;
     }

     public int getRows() {
         return rows;
     }

     public int getSeatsPerRow() {
         return seatPerRow;
     }

     public double getFare() {
         return fare;
     }

     public String getOrigin() {
         return origin;
     }

     public String getDestination() {
         return destination;
     }

     public boolean isAvailable(int userRow, int userSeatPerRow) {

    int row = 0;
    int seatPerRow = 0;

     }
}//end of accessors
4

2 回答 2

1

你的功能

public boolean isAvailable(int userRow, int userSeatPerRow) {

    int row = 0;
    int seatPerRow = 0;

     }

不返回任何东西。那是因为你不知道该放什么,还是你忘记了……?

大概,您会想要一个将座位设置为 false 的“book”方法(在座位 [] [] 数组中稍微切换一下);该isAvailable方法需要访问数组并返回值

return seat[userRow][userSeatPerRow].

当这些是传入的参数时,在您的方法中将row和设置为零,这让我感到困惑。seatPerRow

于 2013-02-12T03:50:43.313 回答
1

这是你想要的?

public boolean isAvailable(int userRow, int userSeatPerRow) {
      return !seat[userRow][userSeatPerRow];
} 

这假设seat[x][y]如果 x 行中的座位,位置 y 被占用,则数组为真

如果座位已被占用,seat[userRow][userSeatPerRow]则为true

当 时!,我们得到反转逻辑,所以true变成false

所以函数返回false的是座位被占用,因为那时它是不可用的。如果座位是空的,则会发生相反的情况。然后函数返回true

要找到第一个可用座位,您可以执行以下操作:

 for (int row = 0; row < getRows(); row++) {
     for (int column = 0; column < getSeatsPerRow(); column++) {
         if (isAvailable(row, column)) {
             System.out.println("Sear " + row +", " + column + " is emply!!!!!!");
         }
     } 
 }

检查是否有空位

public boolean areThereAnyAvailableSeats() {
    for (int row = 0; row < getRows(); row++) {
        for (int column = 0; column < getSeatsPerRow(); column++) {
            if (!seat[userRow][userSeatPerRow]) {
                return true;
            }
        } 
    }
    return false;
} 
于 2013-02-12T03:52:55.120 回答