简体   繁体   中英

Exiting a for loop from an if in the for loop?

I'm getting "A flight is available matching this information" "Passenger's details recorded and booking completed"

but then I'm also getting "There are no flights flying to this destination"

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语句,以确保循环中断。

The issue here occurs when you find a valid flight for your passenger before the loop has finished running through each iteration. setting k to 5 in your if statement is a step in the right direction, but does not work because you then use flights[5] throughout the rest of the block.

You can use a break statement instead of k = 5 , but if you want to ensure your code will be easily maintainable in the future, you can make your intentions clear by using a while loop with a boolean specifying when you are done.

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++;
}

Some people think that using break is fine, and in many cases I agree with them; however, when you come back to look at your code (or someone else does), it's nice to know that a for loop will absolutely execute the number of times that it specifies, but a while loop could exit early. It just makes things easier in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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