【问题标题】:How to create boolean method?如何创建布尔方法?
【发布时间】:2023-03-23 06:21:01
【问题描述】:

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

【问题讨论】:

  • 你尝试了什么?你有什么问题?
  • 问题有点不完整。 “您期望发生什么”,“实际发生什么,输入/输出是什么”。所有好东西都可以帮助我们帮助您。
  • 这家伙实际上是想让我们为他写他的作业。
  • 这只是描述航班的类。我创建了一个二维布尔数组来表示航班上的座位。每个座位要么可用,要么不可用。座位[0][0] 是座位1,座位[0][1] 是座位2,依此类推。因此,如果座位[0][0] 为真,则座位可用。我想我很难掌握在哪个座位上确定真假的概念。它只是一个类文件,而不是应用程序文件,它只是一个由应用程序文件调用以检查座位是否可用的方法。

标签: java arrays loops multidimensional-array


【解决方案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;
} 

【讨论】:

  • 是否返回座位是否可用?这就是让我困惑的地方。因为我还没有用户输入来确定rows和seatsPerRow,这些都是参数,所以我不明白如何使用它们来检查真假?
  • 比如,我会创建一个循环来遍历每个座位或阵列的一部分以检查可用性吗?
  • 好的!我明白那个!这会检查单个座位(给定检查阵列中该位置的参数)的可用性!?!如果我在那之后搜索航班上的任何个可用座位,我会在那里创建一个循环吗?
  • 哦!!!!我现在明白它是如何像一个井字棋盘了。所有这些都可以用相同的方法完成吗?顺便说一句,非常感谢。
  • 想以单一方式打印所有可用座位?
【解决方案2】:

你的功能

public boolean isAvailable(int userRow, int userSeatPerRow) {

    int row = 0;
    int seatPerRow = 0;

     }

不返回任何内容。那是因为您不知道该放什么,还是您忘记了...?

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

return seat[userRow][userSeatPerRow].

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-22
    • 2014-11-25
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多