简体   繁体   English

布尔多维数组与嵌套for循环一起使用-使其停止

[英]Boolean multidimensional arrays working with nested for loops - making it stop

I'm kind of stumped here. 我有点被困在这里。

I'm relatively new to Java/Programming in general. 总的来说,我对Java / Programming比较陌生。 I want to make a program that "books seats" in a 4x4 grid. 我想制作一个在4x4网格中“预订座位”的程序。 This multidimensional array will be of the type boolean so that if the seat is not taken, it returns false, but if it is taken it will return true. 此多维数组将为boolean类型,因此,如果不使用座位,则返回false,但是如果使用座位,则返回true。 I want to be able to specify different seats, so like the first two rows will be a different section than the last two rows. 我希望能够指定不同的座位,所以像前两排将是与后两排不同的部分。 Right now I have a method but it just books the entire first two rows as soon as I call that method (as it should, logically speaking). 现在,我有一个方法,但是只要我调用该方法,它就只会记录整个前两行(从逻辑上讲应该是)。 But I want to only be able to book one seat at a time, so that it will end that for loop as soon as one seat is booked. 但是我只希望一次只能预定一个座位,这样一预定一个座位就可以结束for loop。 If I select that I want to book another seat, it will move to the next available seat in those rows or columns. 如果我选择要预订另一个座位,它将移动到这些行或列中的下一个可用座位。

Here is the code so far: 这是到目前为止的代码:

import javax.swing.JOptionPane;

public class Seats{
int maxRows = 4;
int maxCols = 4;
boolean seating[][] = new boolean[maxRows][maxCols];

String bookSeat = null;

public static void main(String []args){
    Seats seats = new Seats();
    seats.start();

}

public void start(){
    bookSeat = JOptionPane.showInputDialog(null, "Book a seat? (y/n)");
    if(bookSeat.equals("y")){
        bookSeat();
    }else{
        JOptionPane.showMessageDialog(null, "Okay.");       
    }
    displaySeats(seating);
}

private boolean bookSeat(){
    boolean isBooked = false;
    for(int row = 0; row <2; row++){
        for(int col = 0;col<maxCols;col++){
            if (seating[row][col] == false){
                seating[row][col] = true;
                isBooked = true;
            }
        }
    }
    return isBooked;

}

private void displaySeats(boolean[][] anArray){

    String seatTaken;
    int r=0;
    int c=0;
    for(int display=0; display<1; display++){
        for(r=0;r<anArray.length;r++){
            for(c=0;c<anArray.length;c++){
                if (seating[r][c]==false){
                    seatTaken = "O";
                }
                else{
                    seatTaken = "X";
                }
            System.out.print("\t[" + seatTaken + "] \t");

            }
            System.out.println("");

        }
    }       
  }
}
private boolean bookSeat(){
    boolean isBooked = false;
    for(int row = 0; row <2; row++){
        for(int col = 0;col<maxCols;col++){
            if (seating[row][col] == false){
                seating[row][col] = true;
                return true;
            }
        }
    }
    return false;
}

I think what you're looking to do is return once you find an open seat and book it -- as shown in the method above. 我认为您想要做的是找到空位并预订后就返回-如上面的方法所示。 This way the loops won't continue once you book a seat. 这样,一旦您预订了座位,循环就不会继续。

Assuming you mean that you want to exit this method as soon as it finds a seat. 假设您要在找到席位后立即退出此方法。

private boolean bookSeat()
{
  for(int row = 0; row <2; row++)
  {
    for(int col = 0;col<maxCols;col++)
    {
        if (seating[row][col] == false)
        {
            seating[row][col] = true;
            return true;
        }
    }
  }
  return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM