如何创建布尔方法?

时间:2013-02-12 03:47:26

标签: java arrays loops multidimensional-array

我正在尝试创建一个名为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

2 个答案:

答案 0 :(得分:1)

你的功能

public boolean isAvailable(int userRow, int userSeatPerRow) {

    int row = 0;
    int seatPerRow = 0;

     }

不会返回任何内容。那是因为你不知道该放什么,或者你忘了......?

据推测,您可能希望使用“book”方法将座位设置为false(在seat [] []数组中稍微切换一下); isAvailable方法需要访问数组并返回值

return seat[userRow][userSeatPerRow].

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

答案 1 :(得分:1)

这是你在找什么?

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

如果座位在第x行,地点y被占用

,则假设seat[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;
}