A concrete implementation of the Java termination loop body
- 2020-04-01 02:58:16
- OfStack
Write a program that creates an array of strings, runs through the array using a foreach statement, and immediately breaks the loop if the array contains the string "eagle." Create another 2-d array of integer type and iterate through it with double-layer foreach statement. When the first array element less than 60 is found, the whole double-layer loop is immediately interrupted instead of the inner loop.
public class Foreach {
public static void main(String[] args){
System.out.println("n------------- Example of breaking a single layer loop -------------");
//Create an array
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(" Tell me what birds there are before you find the first one. ");
for (String string : array) { //Foreach traverses the array
if (string.equals(" The eagle ")) //If you come across an eagle
break;//Interrupt cycle
System.out.print(" There are: " + string+" "); //Otherwise output array elements
}
System.out.println("n------------- Example of breaking a two-layer loop -------------");
//Create an array of achievements
int[][] myScores = new int[][] { { 67, 78, 63, 22, 66 }, { 55, 68, 78, 95, 44 }, { 95, 97, 92, 93, 81 } };
System.out.println(" The results of this exam: n mathematics t Chinese language and literature t English t The fine arts t history ");
No1: for (int[] is : myScores) { //Traverse the grade chart
for (int i : is) {
System.out.print(i + "t"); //The output result
if (i < 60) { //If you encounter a failure, immediately interrupt all output
System.out.println("n And so on, " + i + " What is it? Why is this failing? ");
break No1;
}
}
System.out.println();
}
}
}
The effect is shown in the figure:
< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201402/2014215162754870.png" >