Java 填充二维布尔数组
我需要逐个填充布尔数组。下面是我用来将第一行填充为true的代码,一个接一个,它正在工作。我想用一个循环或者别的什么来实现这一点,所以我不需要所有的if,else-if语句。有什么建议吗Java 填充二维布尔数组,java,arrays,boolean,fill,Java,Arrays,Boolean,Fill,我需要逐个填充布尔数组。下面是我用来将第一行填充为true的代码,一个接一个,它正在工作。我想用一个循环或者别的什么来实现这一点,所以我不需要所有的if,else-if语句。有什么建议吗 public class Airline { boolean seat[][] = new boolean[2][3]; Scanner input = new Scanner(System.in); public void start() { while (true) { m
public class Airline {
boolean seat[][] = new boolean[2][3];
Scanner input = new Scanner(System.in);
public void start() {
while (true) {
makeReservation();
}
}
public void makeReservation() {
System.out.println("Press 1 or 2");
int klass = input.nextInt();
if (klass == 1) {
firstClassSeat();
} else {
economySeat();
}
}
public void firstClassSeat() // assign a first class seat
{
for (int row = 0; row < seat.length; row++) {
for (int col = 0; col < seat[row].length; col++) {
}
if (seat[0][0] == false)
{
seat[0][0] = true; // assign seat
System.out.println("You now have seat 00 in first class");
break;
}
if (seat[0][1] == false) {
seat[0][1] = true; // assign seat
System.out.println("You now have seat 01 in first class");
break;
} else if (seat[0][2] == false) {
seat[0][2] = true; // assign seat
System.out.println("You now have seat 02 in first class");
break;
} else {
System.out.println("The plane is full");
int val = input.nextInt();
if (val == 1) {
economySeat();
start();
} else {
System.out.println("Thank you and welcome again");
System.exit(0);
}
}
}
}
public void economySeat() // assign an economy seat
{
for (int row = 0; row < seat.length; row++) {
for (int col = 0; col < seat[row].length; col++) {
}
if (seat[1][0] == false)
{
seat[1][0] = true; // assign seat
System.out.println("You now have seat 01 in first class");
break;
}
if (seat[1][1] == false) {
seat[1][1] = true; // assign seat
System.out.println("You now have seat 02 in first class");
break;
} else if (seat[1][2] == false) {
seat[1][2] = true; // assign seat
System.out.println("You now have seat 02 in first class");
break;
} else {
System.out.println("The economy class is full ");
int val = input.nextInt();
if (val == 1) {
economySeat();
start();
} else {
System.out.println("Thank you and welcome again");
System.exit(0);
}
}
}
}
}
下面是我使用第一类方法所做的
public void firstClassSeat() {
for (int row = 0; row < seat.length; row++) {
for (int col = 0; col < seat[row].length; col++) {
if (!seat[row][col])
{
seat[row][col] = true;
System.out.println("You have place number 0" + row + " in the Firstclass");
break;
} else if (seat[0][2]) {
if (seat[1][2]) {
System.out.println("The plane is full, welcome again");
System.exit(0);
}
} else {
System.out.println("First class is full. Economyclass? 1 for yes 2 for no");
int choice = input.nextInt();
if (choice == 1) {
economySeat();
start();
} else {
System.out.println("Thank you and welcome again");
System.exit(0);
}
}
}
}
}
但我不知道怎么做,它只会一个接一个地使数组变为真。这个代码一次需要两个。有什么建议吗?您的代码中有很多非常糟糕的约定,但您需要的是
for(int i = 0; i < seat.length; i++)
for(int j = 0; j < seat[i].length; j++)
seat[i][j] = newBool;
发布全班。我发布了全班。它还没有完成。我只是想知道如何更改代码,这样我就不需要所有的if和else if语句。你想在每一行中都执行true-false-false之类的操作吗?这段代码正是我想要的。我只是想知道是否有其他方法可以代替所有的if和else语句来填充座位如果foo是布尔值,为什么使用iffoo==true。。。?您不需要检查该值是否为false-无论代码运行后发生什么,它都将为true