坚持无限循环。无法弄清楚为什么?

时间:2015-10-21 22:17:46

标签: java

我陷入无限的for循环中,无法弄清楚原因。我将嵌套for循环的边界设置为输入的行和列,但仍然无限地继续。继承我的飞机课和我的主班:

飞机:

public class Plane {
private Passenger[][] pArray;
private String flightNumber;
private int rows;
private int seatsPerRow;

public Plane(String fn, int r, int spr) {
    flightNumber = fn;
    rows = r;
    seatsPerRow = spr;
    pArray = new Passenger[r][spr];
}

public boolean addPassenger(String name, int ro, int sir) {
    boolean result = false;

    if(ro <= rows && sir <= seatsPerRow && pArray[ro][sir] == null){
        pArray[ro][sir] = new Passenger(name);
        System.out.println("Passenger " + name + " was added.");
        result = true;
    }
    return result;
}

public boolean removePassenger(int r, int s){
    boolean result = false;

    if(pArray[r][s] != null){
        pArray[r][s] = null;
        System.out.println("Passenger was removed.");
        result = true;
    } else {
        System.out.println("Invalid seat -- please try again.");
    }
    return result;

}

public void showSeats(){
    System.out.print(" ");
    for (int j = 0; j < pArray[0].length; j++) {
        System.out.printf("|%25d|", j);
    }
    System.out.println();
    // Display the contents of the machine
    for (int i = 0; i < rows; i++) {
        // Display the row label
        System.out.printf("%2d", i);
        for (int j = 0; j < seatsPerRow; j++) {
            if (pArray[i][j] == null) {
                // slot is empty
                System.out.printf("|%25s|", "");
            } else {
                System.out.printf("|%25s|", pArray[i][j].getName());
            }
        }
        System.out.println();
    }
    System.out.println();
}

public void passengerList(){
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < seatsPerRow; j++){
            if(pArray[i][j] != null){
                System.out.println(pArray[i][j]);
            }
        }
    }
}

主:

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String flight;
    int rows;
    int numRows;
    String line;

    System.out.println("Welcome to Oswego Airlines\nEnter a flgiht number: ");
    flight = sc.nextLine();     
    System.out.println("Enter the number of rows: ");
    line = sc.nextLine();
    rows = Integer.parseInt(line);
    System.out.println("Enter the number of seats per row: ");
    line = sc.nextLine();
    numRows = Integer.parseInt(line);

    Plane plane = new Plane(flight, rows, numRows);

    System.out.println("Enter add, remove, seats, list, or quit: ");
    String command = sc.nextLine();

    while(command != "quit"){
        if(command.equalsIgnoreCase("add")){
            System.out.println("Enter passenger name, row, and seat: ");
            String pName = sc.nextLine();
            String[] passenger = pName.split(" ");
            int rowParse = Integer.parseInt(passenger[1]);
            int seatParse = Integer.parseInt(passenger[2]);

            plane.addPassenger(passenger[0], rowParse, seatParse);

            System.out.println("Enter add, remove, seats, list, or quit: ");
            command = sc.nextLine();

        } else if (command.equalsIgnoreCase("remove")){
            System.out.println("Enter row and seat: ");

            String rName = sc.nextLine();
            String[] removal = rName.split(" ");
            int rowParse = Integer.parseInt(removal[0]);
            int seatParse = Integer.parseInt(removal[1]);

            plane.removePassenger(rowParse, seatParse);

            System.out.println("Enter add, remove, seats, list, or quit: ");
            command = sc.nextLine();

        } else if (command.equalsIgnoreCase("seats")){
            plane.showSeats();

            System.out.println("Enter add, remove, seats, list, or quit: ");
            command = sc.nextLine();
        } else if (command.equalsIgnoreCase("list")){
            plane.passengerList();
        } else {
            System.out.println("Unknown command -- please try again.\nEnter add, remove, seats, list, or quit: ");
            command = sc.nextLine();
        }
    }

    System.out.println("Closing Oswego Airlines");








}

}

我和我的朋友已经看过了,但我们似乎无法弄明白。

1 个答案:

答案 0 :(得分:2)

需要使用.equals()检查字符串相等性。换句话说,

while(command != "quit"){

应该是

while(!command.equals("quit")){

前者检查它们是否确实是同一个对象,而不是它们具有相同的值。