1.for loop
Format of 1.1.for
Format: for(1 Initialization statement;2 Conditional judgment statement;3 Conditional control statement){ 4 Loop body statement; } Execute process: 1 2 4 3 2 4 3 2
1.2 Cyclic Output of 10 Advertisements
package com.itheima01; public class Demo01 { public static void main(String[] args) { //Cycle 10 Advertisements for(int i=0; i<10; i++){ // 0 1 2 3 4 5 6 7 8 9 System.out.println("Don't eat watermelon in autumn..."); } for(int i=1; i<=10; i++){ // 1 2 3 4 5 6 7 8 9 10 System.out.println("Eat peaches in autumn..."); } } }
1.3 Cycles 1-5 and 5-1
package com.itheima01; public class Demo02 { public static void main(String[] args) { //Output 1-5 digits for(int i=1; i<=5; i++){ System.out.println(i); } //Output 5-1 digits for(int i=5; i>=1; i--){ System.out.println(i); } } }
1.4 Sum 1-5
package com.itheima01; public class Demo03 { public static void main(String[] args) { //Find the sum of 1-5 int sum = 0; for(int i=1; i<=5; i++){ sum = sum+i; } System.out.println(sum); /* sum = 0+1; sum=1 sum = 1+2; sum=3 sum = 3+3; sum=6 sum = 6+4; sum=10 sum = 10+5; sum=15 */ } }
1.5 Find odd and even numbers
package com.itheima01; public class Demo04 { public static void main(String[] args) { //Loop starts with 0 and ends with 10 for(int i=0; i<=10; i++){ //Judgement statement if(i%2==0){ System.out.println(i+"Is Even"); }else{ System.out.println(i+"is odd"); } } } }
2.while Loop
Format of 2.1 while
Format: Initialization statement; while(Conditional judgment statement){ Loop body statement; Conditional control statement; }
2.2 Output 1-5 and 5-1
package com.itheima01; public class Demo05 { public static void main(String[] args) { //Output 1-5 int i = 1; while(i<=5){ System.out.println(i); i++; } //Output 5-1 int j = 5; while(j>=1){ System.out.println(j); j--; } } }
2.3 Sum 1-100
package com.itheima01; public class Demo06 { public static void main(String[] args) { //while loop finds the sum of 1-100 int sum = 0; int i = 1; while(i<=100){ sum += i; i++; } System.out.println("1 Sum to 100 and Yes" + sum); } }
2.4 Mt. Everest
package com.itheima01; public class Demo07 { public static void main(String[] args) { //Define paper thickness double zhi = 0.0001; //Define mountain height int shan = 8848; //Defines the number of times a variable record is collapsed int count = 0; //Write a while loop while(zhi <= shan) { //Multiply paper thickness by 2 for each fold zhi = zhi*2; //Add 1 per fold count++; } //Final number of prints after the cycle ends System.out.println(count); } }
3.do...while
3.1do...while format
Format: Initialization statement; do { Loop body statement; Conditional control statement; } while(Conditional judgment statement);
3.2 Print 1-5
package com.itheima01; public class Demo08 { public static void main(String[] args) { //Print 1-5 int i = 1; do{ System.out.println(i); i++; }while (i<=5); } }
4. Three cycle differences
for: Recommended when specifying the number of loops while: Recommended when the number of loops is ambiguous Recommended for dead-loop writing while(true){ } do..while: No use scenarios...
One: Different formats
-
The for loop forms a whole;
-
The initialization statements of the while loop and the do_while loop are separated from the loop definition;
-
Initialization and control statements of the while and do_while loops are generally omitted, while for loops are not.
Two: Initialization statements are different
-
Definition location is different;
-
Scopes are different:
for The initialization of the loop is restricted to the internal use of the loop; while Cyclic sum do_while The initialization conditions of the loop can be used outside the loop;
3. Different number of executions of circulatory body
-
The loop body statements of the for loop and the while loop execute 0~n times;
-
The loop body statements of the do_while loop are executed 1 to n times, that is, at least once;
Fourth: Different scenarios
-
for and while loops are interchangeable, while loops are simpler;
-
do_while loops are used when loop body statements need to be executed at least once;
--------
Original Link:
5.Debug code debugging
debug The function is to see how the code is executed,Look at the problem in the code usage: 1.Breakpoint,Where can't point where 2.Right-click Selection debug Function 3.spot F8 Execute Down 4.click stop End Procedure 5.Point Breakpoint,Remove Breakpoint
6. Loop Jump Statement
continue: In a loop,Indicates skipping a loop,Enter Next Cycle break: In a loop,Indicates an end cycle,The entire current loop is over Matters needing attention: 1.Any loop can be used,for while do.while.. 2.Must be used in judgment statements
package com.itheima02; public class Demo01 { public static void main(String[] args) { //Output 1-100, skip multiples of 7 for(int i=1; i<=100; i++){ if(i%7==0){ continue; } System.out.println(i); } } }
7. Loop Nesting
package com.itheima02; public class Demo02 { public static void main(String[] args) { //24 hours a day for(int i=0; i<24; i++){ //60 minutes in an hour for(int j=0; j<60; j++){ System.out.println(i + "hour" + j+"Minute"); } } /* i=0 j=0 0 Hour 0 minutes j=1 0 Hour 1 Minute j=2 0 Hour 2 minutes ... j=59 0 Hours 59 minutes i=1 j=0 1 Hour 0 minutes j=1 1 Hour 1 Minute j=2 1 Hour 2 minutes ... j=59 1 Hours 59 minutes i=23 j=0 23 Hour 0 minutes j=1 23 Hour 1 Minute j=2 23 Hour 2 minutes ... j=59 23 Hours 59 minutes */ } }
package com.itheima02; public class Demo03 { public static void main(String[] args) { //Loop 4 times for 4 lines for(int i=0; i<4; i++){ //Cycle 5 times for a row of 5 stars for(int j=0; j<5; j++) { System.out.print("*"); } //Line Break System.out.println(); } /* ***** ***** ***** ***** */ } }
8.Random Random Numbers
1.Guide Pack import java.util.Random; 2.create object Random r = new Random(); 3.Generate Random Numbers(0-9 Random number of) int a = r.nextInt(10);
package com.itheima02; //Guide Pack import java.util.Random; public class Demo05 { public static void main(String[] args) { //create object Random r = new Random(); //Generate 0-9 random numbers int a = r.nextInt(10); //Print System.out.println(a); //Generate 0-99 random numbers int b = r.nextInt(100); //Print System.out.println(b); //Think, every time you generate numbers from 0 to? What if I want random numbers from 1 to 10? int c = r.nextInt(10) + 1; //Think, if you want to have a random number of 11-20 int d = r.nextInt(10) + 11; //Think regularly if you want to generate random numbers of n~m // r.nextInt(m-n+1) + n; //Want 20-40 random numbers // 0 ~ 20 int e = r.nextInt(21) + 20; } }
Besides:
Random has two constructions:
public Random()
public Random(long seed)
In fact, the first parameterless construction method defaults to the current time as the seed. So what is the seed?
Let's start with Random's next() method:
protected int next(int bits) {
long oldseed, nextseed;
AtomicLong seed = this.seed;
do {
oldseed = seed.get();
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareAndSet(oldseed, nextseed));
return (int)(nextseed >>> (48 - bits));
}
Seed is the seed, and its purpose is to generate a random number.
- What is the difference between the two construction methods?
Now let's look at an example of how these two construction methods differ.
public class RandomDemo {
public static void main(String[] args) { for(int i = 0; i < 5; i++) { Random random = new Random(); for(int j = 0; j < 5; j++) { System.out.print(" " + random.nextInt(10) + ", "); } System.out.println(""); } }
}
The above uses the Random parameterless construction method and runs as follows:
2 0 3 2 5
6 4 1 9 7
9 1 8 3 6
2 5 3 5 6
9 9 9 4 5
You can see that the results vary from time to time. The following uses Random's parametric construction method:
public class RandomDemo {
public static void main(String[] args) { for(int i = 0; i < 5; i++) { Random random = new Random(47); for(int j = 0; j < 5; j++) { System.out.print( + random.nextInt(10) + " "); } System.out.println(""); } }
}
The printout is as follows:
8 5 3 1 1
8 5 3 1 1
8 5 3 1 1
8 5 3 1 1
8 5 3 1 1
This is because the parametric construction method uses the current time as the seed, and each seed is different and more random. The parametric construction method uses the fixed value as the seed, and each output value is the same.
--------
9. Guess Numbers Game
package com.itheima02; import java.util.Random; import java.util.Scanner; public class Demo06 { public static void main(String[] args) { /* The program automatically generates a number between 1 and 100. Use the program to guess what the number is? Give hints according to different situations If the number you guessed is larger than the real number, it tells you that the number you guessed is larger If the number you guessed is smaller than the real number, the number you guessed is smaller If the number guessed is equal to the real number, congratulations on your guess */ //1. Generate a 1-100 random number Random r = new Random(); int num = r.nextInt(100)+1; //loop while (true) { //2. Keyboard input a number Scanner sc = new Scanner(System.in); System.out.println("Please enter a 1-100 Integer:"); int a = sc.nextInt(); //3. Compare size if (a > num) { //3.1 Guess big System.out.println("You guessed it~"); } else if (a < num) { //3.2 Guess Small System.out.println("You guessed it was too small"); } else { //3.3 Guessed System.out.println("Congratulations on your guess!"); //End cycle break; } } } }
RESPECT&LOVE: Progress Together & Grow Together