Java method to implement filters for the body of a loop

  • 2020-04-01 02:58:19
  • OfStack

Write programs that implement loop body filters with the continue statement, filter the "eagle" string, and do the corresponding processing, but discard all code after the continue statement. If an "eagle" string is encountered, a specific processing is performed, then the general processing is skipped using the continue statement.


public class Continue { 
    public static void main(String[] args){ 
        String[] array = new String[] { " egrets ", " Red-crowned cranes ", " orioles ", " The parrot ", " The crow ", " The magpies ", 
                " The eagle ", " The cuckoo ", " The eagle ", " Ash grain bird ", " The eagle ", " The lark " }; 
        System.out.println(" There are many birds in my garden, but recently some eagles came. Please help me to catch them. "); 
        int eagleCount = 0; 
        for (String string : array) {//Foreach traverses the array
            if (string.equals(" The eagle ")) {//If you come across an eagle
                System.out.println(" Found an eagle, caught in the cage. "); 
                eagleCount++; 
                continue;//Interrupt cycle
            } 
            System.out.println(" Search for birds and find: " + string);//Otherwise output array elements
        } 
        System.out.println(" A total of: " + eagleCount + " Eagle. "); 
    } 
} 

The effect is shown in the figure:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201402/2014215161634119.png" >


Related articles: