java 如何创建布尔方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14825064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:30:50  来源:igfitidea点击:

How to create boolean method?

javaarraysloopsmultidimensional-array

提问by user1368970

I'm trying to create a boolean method called isAvailable that accepts a seat number on a flight as a parameter, returns a boolean that indicates whether that seat is available. With code similar to a tic-tac-toe program, the code will convert a seat number like 20 into a row and column index for the two-dimensional array. It returns true if there are anyavailable seats on the flight.

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

回答by Floris

Your function

你的职能

public boolean isAvailable(int userRow, int userSeatPerRow) {

    int row = 0;
    int seatPerRow = 0;

     }

does not return anything. Is that because you don't know what to put there, or did you forget...?

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

Presumably, you will want to have a "book" method that sets a seat to false (toggle a bit in the seat[][] array); the isAvailablemethod needs to access the array and return the value

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

return seat[userRow][userSeatPerRow].

Setting rowand seatPerRowto zero in your method, when these are the parameters being passed in, leaves me bewildered.

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

回答by user000001

Is this what you are looking for?

这是你想要的?

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

This assumes that seat[x][y]array is true if seat in row x, place y is occupied

seat[x][y]如果第 x 排的座位被占用,则假设数组为真

if the seat is occupied, then seat[userRow][userSeatPerRow]is true.

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

When the !, we get the invert the logic, so truebecomes false.

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

So the function returns falseis the seat is occupied, since then it is not available. The opposite happens if the seat is empty. Then the function returns true

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

To find the first available seat you would do something like this:

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

 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!!!!!!");
         }
     } 
 }

To check if there are any available seats

检查是否有任何可用座位

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;
}