gpt4 book ai didi

java - 陷入无限循环。不明白为什么?

转载 作者:行者123 更新时间:2023-12-02 04:16:22 26 4
gpt4 key购买 nike

我陷入了无限的 for 循环,无法弄清楚为什么。我将嵌套 for 循环的边界设置为输入的行和列,但仍然无限地进行。这是我的 Plane 类和我的主类:

飞机:

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)类主要{

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");








}

}

我和我的 friend 已经看过它,但我们似乎无法弄清楚。

最佳答案

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

while(command != "quit"){

应该是

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

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

关于java - 陷入无限循环。不明白为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com