繁体   English   中英

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

[英]Exiting a for loop from an if in the 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语句,以确保循环中断。

当您在循环完成每次迭代之前找到适合您的乘客的有效航班时,就会出现此问题。 在if语句中将k设置为5是朝正确方向迈出的一步,但不起作用,因为您随后将在该块的其余部分中使用flights[5]

可以使用break语句而不是k = 5 ,但是如果您想确保以后的代码易于维护,则可以通过使用while循环和布尔值来指定完成时间,从而使意图更清晰。

int k = 0;
bool done = false;

while (!done && k <= 4)
{
    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");

                // added this line:
                done = true;        
            }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");
    }

    k++;
}

有人认为使用break很好,在很多情况下我都同意。 但是,当您返回查看代码(或其他人查看)时,很高兴知道for循环将绝对执行它指定的次数,但是while循环可能会提前退出。 它只是使将来的事情变得容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM