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

时间:2014-02-26 19:27:43

标签: 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");
    }

}

2 个答案:

答案 0 :(得分:4)

您可以在break条件中添加if语句,以确保循环中断。

答案 1 :(得分:0)

如果在循环完成每次迭代之前为您的乘客找到有效航班,则会出现此问题。在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循环可以早点退出。它只会让事情变得更容易。