我收到“有与此信息匹配的航类” “已记录乘客详细信息并完成预订”
但随后我也收到“没有飞往该目的地的航类”
for (int k = 0;k <=4; k++)
{
if (destination.equalsIgnoreCase(flights[k].getDestination()))
{
k = 5;
System.out.print("\nEnter desired day of departure: ");
day = scan.nextLine();
System.out.println("\f");
if (day.equalsIgnoreCase(flights[k].getDay()))
{
if (flights[k].getBookedSeats() < 100)
{
passengers[0 + bookedSeats].setName(name);
passengers[0 + bookedSeats].setAddress(address);
passengers[0 + bookedSeats].setEmail(email);
passengers[0 + bookedSeats].setOnFlight(k);
flights[k].increaseBookedSeats();
System.out.println("\nA flight is available matching this information");
System.out.println("Passenger's details recorded and booking completed");
}else{
System.out.println("\nThere are no seats available on this flight");
}
}else
{
System.out.println("\nThere are no flights flying to this destination on this day");
}
}else if(!destination.equalsIgnoreCase(flights[k].getDestination()) && k==4)
{
System.out.println("\nThere are no flights flying to this destination");
}
}
最佳答案
您可以在 if
条件内添加 break
语句以确保循环中断。
关于java - 从 for 循环中的 if 退出 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051276/