Nineth Chapter
Pre-word:
- Triangle Circle Diamond Password row
- Triangle Loop Diamond Password Line
1:2 Heavy Cycle structure:
Grammar:
Public classXIA2 { Public Static voidMain (string[] args) {//While --- while while(condition 1) { while(Condition 2) {}}//Do ---whlie Do{ Do{ } while(condition 1); } while(condition 2); //For and for for(Condition 1 ) { for(Condition 2) {}}//While and while while(Condition 1){ for(Condition 2) {} }}}
Two: Using a double cycle:
ImportJava.util.Scanner; Public classXIA2 { Public Static voidMain (string[] args) {intRows=0; System.out.print ("Please enter the line number of the right angle triangle:"); Scanner input=NewScanner (system.in); Rows=Input.nextint (); for(inti = 1; I <=rows; i++) { for(intj = 1; J <=2*i-1; J + +) {System.out.print ("*"); } System.out.println ("\ n"); } }}
Three: Jump statement advanced
for (.....) {for(...) {...... Continue ; ....... } .......}
Break statement:
for (.....) {for(...) { ...... Break ; ...... } ......}
ImportJava.util.Scanner; Public classxia8 { Public Static voidMain (string[] args) {intcount=0;//counter, record bought a few clothes altogetherString Choice; Scanner input=NewScanner (system.in); for(inti = 0; I < 5; i++) {System.out.println ("Welcome to the first" + (i+1) + "Home Shop"); for(intj = 0; J <3; J + +) {System.out.println ("To leave (y/n)"); Choice=Input.nextline (); if("Y". Equals (choice)) { Break; } System.out.println ("Bought a piece of clothing."); Count++; } System.out.println ("Close to the hotel"); } System.out.println ("Bought a total of" +count+ "clothes"); Choice=Input.nextline (); }}
Summarize:
A double cycle is a cycle in which the body of a loop contains another complete loop structure.
The execution of the Break...continue statement control program can be used in a double loop
2016-07-09
16:37:14
Cheng.java
Advanced Loop structure