java - 从 for 循环中的 if 退出 for 循环?

标签 java loops if-statement for-loop

我收到“有与此信息匹配的航类” “已记录乘客详细信息并完成预订”

但随后我也收到“没有飞往该目的地的航类”

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/

相关文章:

Mysql Group BY 带 if 语句

java - 打开一个有代理设置的网站-Java

java - 关于使用 Collection 对象时的 Java 堆空间

java - 转换颜色

c - 如何编写正确的 do..while 循环?

python - 避免在我的代码中使用多个(未嵌套的)if 语句

java - 两个线程执行两个 `synchronized` 方法?

r - 如何在 R 中构建一次汇总多个值的表

ios - 具有两个未知值的 Swift 范围运算符

php - 仅当行少于 5 时才选择 MYSQL