It involves creating a string array, traversing a foreach statement, and determining whether to interrupt a loop.
1. Interrupt a single-layer Loop
Package com. lixiyu; public class BreakCyc {public static void main (String [] args) {String arr [] = {"tiger", "White Crane", "Pigeon", "e ", "Crow"}; System. out. println ("tell me what birds are there before you find the first e. "); For (String s: arr) {// traverses the array if (s. equals ("Crow") {break;} System. out. println ("with" + s );}}}
If (s. equals ("Crow") {break;} indicates that when the element is traversed to the crow, the loop ends and the output is.
2. Interrupt a double loop
Implementation:
------------- Example of interrupting the double-layer loop ----------- baby's score in this exam: mathematics, Chinese, English, and art history 67 78 63 22. What is the score of 22? Why does it fail?
Code details:
Package com. lixiyu; public class BreakCy {public static void main (String [] args) {System. out. println ("\ n --------------- example of interrupting a double loop -------------"); // 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 Exam Score: \ n mathematics \ t language \ t English \ t art \ t history"); No1: for (int [] is: myScores) {// traverse the score table for (int I: is) {System. out. print (I + "\ t" ); // Output result if (I <60) {// if the System fails, all output systems are immediately interrupted. out. println ("\ n, etc.," + I +? Why does it fail? "); Break No1 ;}} System. out. println ();}}}
The idea is to create a multi-dimensional array first, and then use the foreach statement to traverse cyclically, judge the elements smaller than 60, and interrupt the entire double-layer loop.
This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1302906