对于循环不破坏

时间:2018-01-15 14:44:08

标签: java for-loop

如何编写代码,以便在满足任何if条件时,for循环中断。我还想确保它不会检查任何其他if语句或else if

   //for loop to print out the specific user names 
    for (int i = 0; i < customerList.size(); i++) {

        //if statement to check input and set the method requirements to 0
        if (checkInput()) {

            //if users name is found and the customers totalpoints are 
            //greater than 5000 run this if statement and print out name and bonus and end for loop
            if (personName.equals(customerList.get(i).customerName)
                    && (customerList.get(i).totalPoints >= 5000)) {

                outputArea.append(personName + "'s Total Points are:"
                        + customerList.get(i).totalPoints + "  Bonus:1000");

                break;
                //if the users name is found but they dont have over 5000 points run this statement 
                //and end for loop
            } else if (personName.equals(customerList.get(i).customerName)) {
                outputArea.append(personName + "'s Total Points are:"
                        + customerList.get(i).totalPoints + "  Bonus:0");
                break;
            } 

        }
    }

1 个答案:

答案 0 :(得分:0)

取决于“for循环结束”的含义:

如果要退出整个for循环使用

break;

因为你已经在你的代码中,我想你想跳到循环的下一次迭代。为此用途

continue;