Introduction to java (7) | Loop structure
Start from scratch!!!
Last issue was an explanation of the basic use of methods and a practice of overloading methods.
This is an explanation of the structure of loops. What do you know about for loops?How should they be used?Hope you have a better answer after reading the following!
Cyclic structure
1,for
Loop structure is a kind of program structure that needs to be executed repeatedly in a program.
It is determined by the conditions in the loop body whether to continue a function or exit the loop.
According to the judgment conditions, the circular structure can also be subdivided into the circular structure of judgment before execution and the circular structure of judgment after execution.
package cn.qile; public class Test1_For { public static void main(String[] args) { //Print 0 to 10 // For (start of loop; condition of loop; change condition) { //i Record each value obtained for(int i = 0 ; i <= 10 ; i++) { System.out.println(i); } } }1.3 Exercise 2: Print 7,77,777,7777,
//Print 7,77,777,7777, for( int i = 7 ; i <= 7777 ; i=i*10+7) { System.out.print(i+","); //Organize data formats separated by commas with a comma }
1.4 Nested for Loop
Based on the conditions of the outer layer, judge whether the inner layer can be executed. If it can be executed, loop through the inner code before proceeding with the outer layer.
Form 1.4.1 For (start of loop; condition of loop; change condition){//outer loop For (start of loop; condition of loop; change condition){//inner loop Circulatory body } } 1.4.2 Exercise 3: Starter Casespackage cn.qile; public class Test3_For2 { public static void main(String[] args) { //Summary 1: Execute the outer loop once and the inner loop several times //When i=1 is, the value of output I is 1, and the value of output j is 12345 //When i=2 is, the value of output I is 2, and the value of output j is 12345 //When i=3 is, the value of output I is 3, and the value of output j is 12345 for (int i = 1; i <= 3; i++) {//Outer loop, 3 times System.out.println(i); for (int j = 1; j <= 5; j++) {//Inner loop, executed 5 times System.out.println(j); } } //Summary 2: Outer loop control rows, inner loop control columns //Outer loop can execute 5 times, so 5 lines can be printed //The inner loop can execute three times, so you can print three columns per row for (int i = 1; i <= 5; i++) {//Outer loop, execute 5 times for (int j = 1; j <= 3; j++) {//Inner loop, 3 times System.out.print("*"); } System.out.println();//Line Break } } }1.4.3 Exercise 4: Print left right triangle
package cn.qile; public class Test4_For3 { public static void main(String[] args) { /* * ** *** **** ***** */ //When i = 1, prepare to print the first row, but print several columns, the inner loop determines.J<=1, can execute once, so the first line prints a *, breaking the line. //When i = 2, prepare to print the second line, but print several columns, the inner loop determines.J<=2, can execute twice, so the second line prints two*, breaking the line. //... for(int i = 1 ; i <= 5 ; i++) {//Outer loop, execute 5 times for(int j = 1 ; j <= i ; j++) {//Internal circulation, change is consistent with external circulation, j changes with i System.out.print("*");//Show on the same line* } System.out.println();//Line Break } } }1.4.4 Exercise 5: Print 99 Multiplication Table
package cn.qile; public class Test5_Jiujiu { public static void main(String[] args) { /* 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 */ //99 Multiplication Table for (int i = 1; i <= 9; i++) {//Outer loop, 9 times for (int j = 1; j <= i; j++) {//Internal circulation, change is consistent with external circulation, j changes with i // System.out.print("2*1=2"); // Where the first number is row i, the second number is column j, and the third number is product //If the string needs to be stitched dynamically before and after the data, "+?+" System.out.print(i + "*" + j + "=" + i * j + " "); } System.out.println();//Line Break } } }
2,while
2.1 Format While (execution condition){ Code... } 2.2 Exercise 6: Guess NumbersGenerate a random number
And the user has been typing,
Number comparison.
package cn.qile; import java.util.Random; import java.util.Scanner; public class Test6_While { public static void main(String[] args) { //1. Let the program generate random numbers. //nextInt(n) -- Generates random numbers within n, starting at 0 int random = new Random().nextInt(100) + 1; System.out.println("Random number:"+random); //2. Call method() to complete the number guessing game method(random);//random is an argument } //Creating method (random) R is a formal parameter public static void method(int r) { //3. Always accept numbers entered by users // Dead cycle for (int I = 1; I >= 1; i+) {//for // While (r > 50) {//If r > 50 is satisfied, the loop is executed while (true) {//while(true) -- Dead loop, program exit must be set System.out.print("Please enter:"); int input = new Scanner(System.in).nextInt(); //4. Determine the relationship between input values and random numbers if (input > r) { System.out.println("Big"); } else if (input < r) { System.out.println("Small"); } else if (input == r) { System.out.println("Medium"); break;//Set the exit!!!! } } } }
3,do-while
Execute before Judge
3.1 Format do{ Code... } while (condition of execution); 3.2 Exercise 7: Transforming Guess NumbersProduces a random number that is compared to the number the user has been entering.
package cn.qile; import java.util.Random; import java.util.Scanner; public class Test7_doWhile { public static void main(String[] args) { //1. Let the program generate random numbers. //nextInt(n) -- Generates random numbers within n, starting at 0 int random = new Random().nextInt(100) + 1; System.out.println("Random number:"+random); //2. Call method() to complete the guess number game - - in a while loop // method(random);//random is an argument //Modify Guess Number Game - loop with do...while method2(random); } //Create method2(random) - to do...while loop //do...while loop guarantees code to execute at least once.--Execute before you judge public static void method2(int r) { do {//while(true) -- Dead loop, program exit must be set System.out.print("Please enter:"); int input = new Scanner(System.in).nextInt(); //4. Determine the relationship between input values and random numbers if (input > r) { System.out.println("Big"); } else if (input < r) { System.out.println("Small"); } else if (input == r) { System.out.println("Medium"); break;//Set the exit!!!! } } while (true); } //Creating method (random) R is a formal parameter - - in a while loop //while must meet the conditions before it can be executed--judge first, execute public static void method(int r) { //3. Always accept numbers entered by users // Dead cycle for (int I = 1; I >= 1; i+) {//for // While (r > 50) {//If r > 50 is satisfied, the loop is executed while (true) {//while(true) -- Dead loop, program exit must be set int input = new Scanner(System.in).nextInt(); //4. Determine the relationship between input values and random numbers if (input > r) { System.out.println("Big"); } else if (input < r) { System.out.println("Small"); } else if (input == r) { System.out.println("Medium"); break;//Set the exit!!!! } } } }
Next issue: Getting started with java (8) | break and continue
Focus on [Kele is not a farmer], reply [Get started with java] and watch the next issue ahead of time!!
Happy waiting for you