Java 如何创建布尔方法?
我试图创建一个名为isAvailable的布尔方法,它接受航班上的座位号作为参数,返回一个布尔值,指示该座位是否可用。使用类似于tic-tac-toe程序的代码,该代码将把座位号(如20)转换为二维数组的行和列索引。如果航班上有空位,则返回trueJava 如何创建布尔方法?,java,arrays,loops,multidimensional-array,Java,Arrays,Loops,Multidimensional Array,我试图创建一个名为isAvailable的布尔方法,它接受航班上的座位号作为参数,返回一个布尔值,指示该座位是否可用。使用类似于tic-tac-toe程序的代码,该代码将把座位号(如20)转换为二维数组的行和列索引。如果航班上有空位,则返回true public class Flight { /* * declare instance variables * each flight object that is created will have its
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
你的职能
public boolean isAvailable(int userRow, int userSeatPerRow) {
int row = 0;
int seatPerRow = 0;
}
不返回任何内容。是因为你不知道该放什么,还是你忘了
您可能希望有一个“book”方法,将seat设置为false(在seat[]数组中切换一位);isAvailable
方法需要访问数组并返回值
return seat[userRow][userSeatPerRow].
在你的方法中将
行
和座位错误
设置为零,当这些是传入的参数时,让我感到困惑。这就是你要找的吗
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!!!!!!");
}
}
}
for(int row=0;row
检查是否有空位
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;
}
public boolean AreThereanyAvailableSets(){
对于(int row=0;row
您尝试了什么?你们有什么问题?问题有点不完整。“你期望发生什么”,“实际发生了什么,输入/输出是什么”。所有的好东西都能帮助我们。那个家伙实际上想让我们为他写作业。这只是描述航班的课程。我创建了一个二维布尔数组来表示航班上的座位。每个座位都有空或没有空。座位[0][0]是座位1,座位[0][1]是座位2,依此类推。因此,如果seat[0][0]为真,则该seat可用。我想我很难理解在哪个座位上判断对错的概念。它只是一个类文件,而不是应用程序文件,它只是应用程序文件调用的一个方法,用于检查座位是否可用。这是否返回座位是否可用?这就是让我困惑的地方。因为我还没有得到用户输入来确定rows和seatsPerRow,这些就是参数,那么我不知道如何使用它们来检查是真是假?比如,我会创建一个循环来遍历每个座位或阵列的一部分来检查可用性吗?好的!我明白!这将检查单个座椅(给定用于检查阵列中该点的参数)的可用性!?!如果在那之后我要搜索航班上的任何可用座位,我会在那里创建一个循环吗?哦!!!我现在明白了,这就像是一个井字游戏。所有这些都可以用同样的方法完成吗?顺便说一句,非常感谢。想用单张方式打印所有可用的座位吗?