改变循环内部条件

时间:2015-02-21 00:41:10

标签: java

所以我试图建立一个飞机座椅系统。我遇到麻烦的特殊功能是团体座位。预订程序应该找到座位排中足以容纳该组的第一排座位,或者如果不存在这样的座位排,则找到任何座位排中具有最多可用座位数的行,填满它与该组的成员一起重复该过程(找到具有最多可用席位的行),直到该组的所有成员都已就位。 这是我尝试过的:

    public void addGroup(){
    System.out.print("Group Name: ");
    String group = sc.nextLine();
    System.out.print("Names:" );
    String names = sc.nextLine();
    System.out.print("Service Class: ");
    String serviceClass = sc.nextLine();    

    //int tempCount = count;
    String [] passengers = names.split(",");
    int numPassengers = passengers.length;
   // int count = 1;
    boolean foundFullSeating = false;
    boolean foundPartialSeating = false;

    int rowIndex = 0;
    int taken = 0;
    int emptySeats = 0;
    //add more
    int i;
    int rows = 2;
    int person = 0;
    int mostSeats = 0;
    int availableIndex = 0;

    if(serviceClass.equalsIgnoreCase("First")){
        for(String s: cabin.getFirstClass()){
            if(s == "X"){
                taken++;
            }
        }
        if(cabin.getFirstClass().size() - taken < numPassengers){
            System.out.println("Not enough seating available for group.");
            return;
        }

    if(foundFullSeating == false){

        for(i = rowIndex; i < rowIndex + 4; i++){
            System.out.println("i = " + i);
            System.out.println("cabin.getFirstClass().get(i) = " + cabin.getFirstClass().get(i));
            if(!cabin.getFirstClass().get(i).contains("X")){
                System.out.println("Found one!");
                emptySeats++;
            }
            System.out.println("emptySeats = " + emptySeats + "numPassengers = " + numPassengers);
            if(emptySeats == numPassengers){
                System.out.println("Everyone fits in one row!");
                foundFullSeating = true;
                foundPartialSeating = true;

            }
            if(i == 3){
                if(rows > 1){
                    emptySeats = 0;
                    i = 0;
                    rowIndex += 4; //moves on to the next row
                    rows--;
                }
            }

        }

    } //end trying to find seating for all passengers in one row

    if(foundPartialSeating == false){
        System.out.println("Line# 162");
        if(numPassengers != 0){
            rowIndex = 0; //start from the beginning
            emptySeats = 0;
            mostSeats = 0;
            availableIndex = 0;
            for(int k = rowIndex; k < rowIndex + 4; k++){
                if(cabin.getFirstClass().get(k) != "X"){
                    emptySeats++;
                }
                if(k == 3){
                    if(emptySeats > mostSeats){
                        mostSeats = emptySeats; 
                        availableIndex = rowIndex; //stores row index with most available seats
                    }

                    if(rows > 1){
                        emptySeats = 0;
                        i = 0;
                        rowIndex += 4; //moves on to the next row
                        rows--;
                    }

                }

            }
            for(int j = availableIndex; j < availableIndex + 4; j++){
                if(cabin.getFirstClass().get(j) != "X"){
                    cabin.getReservedFirst().set(j, cabin.getFirstClass().get(j) + ", G, " + passengers[person]); //adds the passengers seat
                    cabin.getFirstClass().set(j , "X"); // marks seat as taken
                    numPassengers--;
                    person++;
                    System.out.println("Seated at " + cabin.getReservedFirst().get(j));
                    if(numPassengers == 0){
                        foundPartialSeating = true; //seated everyone
                    }
                }
            }
        }
    }

    if(foundFullSeating){
        //System.out.println("rowIndex = " + rowIndex);
        //seat passengers
        for(int j = rowIndex; j < rowIndex + 4; j++ ){
            if(numPassengers > 0){
                //System.out.println("numPassengers = " + numPassengers);
                if(cabin.getFirstClass().get(j) != "X"){
                    cabin.getReservedFirst().set(j, cabin.getFirstClass().get(j) + ", G, " + passengers[person]); //adds the passengers seat
                    cabin.getFirstClass().set(j , "X"); // marks seat as taken
                    numPassengers--;
                    person++;
                    System.out.println("Seated at " + cabin.getReservedFirst().get(j));
                }
            }
        }
    }


    }

头等舱有8个座位(每排2行×4个座位)。我的调试语句产生以下内容:

i = 0
cabin.getFirstClass().get(i) = 1A
Found one!
emptySeats = 1numPassengers = 3
i = 1
cabin.getFirstClass().get(i) = X
emptySeats = 1numPassengers = 3
i = 2
cabin.getFirstClass().get(i) = X
emptySeats = 1numPassengers = 3
i = 3
cabin.getFirstClass().get(i) = 1D
Found one!
emptySeats = 2numPassengers = 3
i = 1
cabin.getFirstClass().get(i) = X
emptySeats = 0numPassengers = 3
i = 2
cabin.getFirstClass().get(i) = X
emptySeats = 0numPassengers = 3
i = 3
cabin.getFirstClass().get(i) = 1D
Found one!
emptySeats = 1numPassengers = 3
i = 4
cabin.getFirstClass().get(i) = 2A
Found one!
emptySeats = 2numPassengers = 3
i = 5
cabin.getFirstClass().get(i) = X
emptySeats = 2numPassengers = 3
i = 6
cabin.getFirstClass().get(i) = X
emptySeats = 2numPassengers = 3
i = 7
cabin.getFirstClass().get(i) = 2D
Found one!
emptySeats = 3numPassengers = 3
Everyone fits in one row!

正如您所看到的,它似乎无法正确处理行的更改。有人可以帮我解决一下发生了什么事吗?我真的很感激。

1 个答案:

答案 0 :(得分:0)

如果我正确理解系统的设置方式,则cabin.getFirstClase()索引中的每组4个属于1行。所以,我认为只看一行的代码看起来应该更像这样:      int emptyRow = -1;      if(foundFullSeating == false){

    for(i = 0; i < 4*rows; i++){
        System.out.println("i = " + i);
        System.out.println("cabin.getFirstClass().get(i) = " + cabin.getFirstClass().get(i));
        if(!cabin.getFirstClass().get(i).contains("X")){
            System.out.println("Found one!");
            emptySeats++;
        }
        System.out.println("emptySeats = " + emptySeats + "numPassengers = " + numPassengers);
        if(emptySeats >= numPassengers){
            System.out.println("Everyone fits in one row!");
            foundFullSeating = true;
            foundPartialSeating = true;

        }
        if(i%4 == 3){
            emptySeats == 0;
            emptyRow = i / 4;
        }

    }

} //end trying to find seating for all passengers in one row

对于部分座位算法,如果您有一个行对象可能会更容易,并且在该行对象中它可以存储座位数,这样可以更容易地找出哪个行拥有最多的席位可用。您可以为基于availableSeats后跟rowNumber返回的行创建一个Comparator,然后将行放在PriorityQueue中并从该队列中弹出,直到填充了足够的席位。如果你以这种方式实现它,这也可以用于查看它们是否都适合一行。