When writing a program, you first create an array of strings. When traversing with the foreach statement, if you find that the array contains the string "eagle", the loop will be interrupted immediately. Create another two-dimensional array of integer type and loop through it using a double-layer foreach statement. When the first array element smaller than 60 is found, the entire double-layer loop is immediately interrupted, rather than the inner loop.
The code copy is as follows:
public class Foreach {
public static void main(String[] args){
System.out.println("/n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Create an array
String[] array = new String[] { "Egret", "Red-crowned Crane", "Original", "Parrot", "Crow", "Magpie",
"Eagle", "Cuckoo", "Eagle", "Gray-patterned Bird", "Eagle", "Lag" };
System.out.println("Tell me what birds are there before you find the first eagle.");
for (String string : array) { // foreach traverses the array
if (string.equals("Eagle")) // If you encounter an eagle
break;// interrupt the loop
System.out.print("has:" + string+" "); // Otherwise, output array elements
}
System.out.println("/n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Create a score array
int[][] myScores = new int[][] { { 67, 78, 63, 22, 66 }, { 55, 68, 78, 95, 44 }, { 95, 97, 92, 93, 81 } } ;
System.out.println("Baby's test scores: /nMathematics/tChinese/tEnglish/tFine Arts/tHistory");
No1: for (int[] is : myScores) { // traverse the score table
for (int i : is) {
System.out.print(i + "/t"); // Output score
if (i < 60) { // If you encounter a failed middle class, interrupt all outputs immediately
System.out.println("/n, etc., " + i + "What is the score? Why does this fail?");
break No1;
}
}
System.out.println();
}
}
}
The effect is shown in the figure: