6-Java basic syntax 4-loop structure

Posted by wild_dog on Thu, 30 Dec 2021 04:00:57 +0100

1. for loop statement

The characteristics of circular structure: do something repeatedly and have the signs of start and end

//Format:
for(Initialization statement;Environment judgment statement;Conditional control statement){
  Loop body statement;
}
/*Execution process
         ①: Execute initialization statement
         ②: Execute the conditional judgment statement to see whether the result is true or false
               If false, the loop ends;
               If true, continue to execute;
         ③: Execute loop body statement
         ④: Execute conditional control statements
         ⑤: Go back to ② and continue
*/
//example
public class hello{
    public static void main(String[] args){
    //Requirements: output hello world five times on the console
    for(int i=0;i<5;i++) {
    	System.out.println("hello world");
    }
    }
}
/*Dead loop format
foe(::){
Loop body statement;
}
ctrl+c End dead cycle
*/

1. Case: summation

Requirements: sum the data between 1-5, and output the sum result on the console

public class hello{
    public static void main(String[] args){
    //Requirements: sum the data between 1-5, and output the sum result on the console
    	int sum = 0;//Define and initialize sum
    for(int i=1;i<=5;i++) {
    	sum+=i;
    }
    System.out.println("sum:"+sum);
    }
}

2. Case: even sum

Requirements: find the even sum between 1-100, and output the sum result on the console

public class hello{
    public static void main(String[] args){
    //Requirements: find the even sum between 1-100, and output the sum result on the console
    	int sum = 0;//Define and initialize sum
    for(int i=1;i<=100;i++) {
    	if((i%2)==0) {
    		sum+=i;
    	}
    }
    System.out.println("sum:"+sum);
    }
}

3. Case: daffodils

Requirement: output all "daffodils" on the console

Number of daffodils: ①: the number of daffodils is a three digit number

②: the cube sum of the number of daffodils, ten digits and hundred digits is equal to the original number.

Tip: how to calculate the value in the specified bit of any number:

First use the division operation to move the required number to one bit, and then use the remainder operation to get the value on the last bit;

For example, take 2 on the tenth bit of 123, divide 10 first, and get 12; Then take the remainder of 12 to get 2

public class hello{
    public static void main(String[] args){
    //Requirement: output all "daffodils" on the console
    	
    for(int i=100;i<1000;i++) {
    	int a = i%10;//Bit
    	int b = (i/10)%10;//Ten
    	int c = i/100;//Hundredth
    	if((a*a*a+b*b*b+c*c*c)==i) {
    		System.out.println(i+":It's daffodils");
    	}
      }
    }
  }

2. while loop statement

//Basic format
while(Conditional judgment statement){
  Loop body statement;
  Conditional control statement;
}
/*Execution process:
          ①: Execute initialization statement
          ②: Execute the conditional judgment statement to see whether the result is true or false
                 If false, the loop ends
                 If true, continue
          ③: Execute loop body statement
          ④: Execute conditional control statements
          ⑤: Go back to 2 and continue
*/
public class hello{
    public static void main(String[] args){
    //Requirements: output hello world five times in a row on the console
    	int i = 0;
    	while(i<5) {
    		System.out.println("hello world");
    		i++;
    	}
    }
  }
/*Dead loop format:
while(true){
Loop body statement;
}
ctrl+c End dead cycle
*/

1. Case: Mount Everest

Demand: the highest peak in the world is Mount Everest (8844.43 M = 8844431 mm). I have a piece of paper large enough, and its thickness is 0.1 mm. How many times can I fold it to the height of Mount Everest?

public class hello{
    public static void main(String[] args){
    //Demand: the highest peak in the world is Mount Everest (8844.43 M = 8844431 mm). I have a piece of paper large enough, and its thickness is 0.1 mm. How many times can I fold it to the height of Mount Everest?
    	double paperh = 0.1;//Record the height of the paper
    	int zm = 8844431;//The height of Mount Everest
    	int n = 0;//Record the number of folds
    	while(paperh<=zm) {
    		paperh *= 2;
    		n++;
    	}
    	System.out.println("Folding required:"+n+"second");
    }
  }

3. do... while loop statement

//Format:
do{
   Loop body statement;
  Conditional control statement;
}while(Conditional judgment statement);
/*Execution process:
         ①: Execute initialization statement
         ②: Execute loop body statement
         ③: Execute conditional control statements
         ④: Execute the conditional judgment statement to see whether the result is true or false
            If true, continue to execute;
            If false, end the loop;
         ⑤: Go back to ② continue
*/
//example
public class hello{
    public static void main(String[] args){
    //Requirement: output hello world five times
    	int i = 0;
    	do {
    		System.out.println("hello world");
    		i++;
    	}while(i<5);
    }
  }
/*be careful:
      do......while The difference between loop and for, while loop: whether the conditional judgment statement is true or not, the do... While loop body statement must be executed at least once.
*/
/*Dead loop format
do{
Loop body statement;
}while(true);
ctrl+c End dead cycle
*/

4. Jump control statement

//Continue is used in a loop. Based on condition control, skip the execution of the contents of a loop body and continue to execute the next loop
//break is used in a loop. Based on conditional control, it ends the current whole loop and executes the statements behind the loop body

5. Loop nesting

Requirements: output the hours and minutes of the day on the console

Range of hours and minutes: 0 < = hours < 24;

0 < = min < 60;

public class hello{
    public static void main(String[] args){
    //Requirements: output the hours and minutes of the day on the console
    	for(int i=0;i<24;i++) {               //Outer circulation control hour
    		for(int j=0;j<60;j++) {            //Inner circulation control minutes
    			System.out.println("Hours and minutes of the day:"+i+"Time"+j+"branch");
    			
    		}
    	}
  }
}

6. Random

Random function: used to generate a random number

//Use steps
//① Guide Package
import java.util.Random();
//② Create object: r is the variable name, which can be changed, and the rest cannot be changed
Random r = new Random;
//③ Get random number: number is the variable name, which can be changed, the number 10 can be changed, and the rest cannot be changed
int number = r.nextInt(10);//10 indicates the range of data obtained: [0,10)
//example
//① Guide Package
import java.util.Random;
public class hello{
    public static void main(String[] args){
    	//② Create object: r is the variable name, which can be changed, and the rest cannot be changed
    	Random r = new Random();
    	//③ Get random number: number is the variable name, which can be changed, the number 10 can be changed, and the rest cannot be changed
    	int number = r.nextInt(10);//10 indicates the range of data obtained: [0,10)
    	//Output random number
    	System.out.println(number);
      
      //Requirement: randomly generate a random number from 1 to 100
      int a = r.nextInt(100)+1;
      System.out.println(a);
  }
}

1. Case: guess the number

Requirements: the program automatically generates a number between 1-100, and uses the program to guess the number.

When you guess wrong, give corresponding tips according to different situations

①: if the guessed number is larger than the real number, it indicates that the guessed number is larger.

②: if the guessed number is smaller than the real number, it indicates that the guessed number is smaller.

③: if the number you guessed is equal to the real number, you will be prompted to congratulate you on your correct guess

import java.util.Scanner;
import java.util.Random;
public class hello{
    public static void main(String[] args){
    	//Requirements: the program automatically generates a number between 1-100, and uses the program to guess the number.
    	Random r = new Random(100)+1;
    	Scanner sc = new Scanner(System.in);
    	int number = r.nextInt(10);//Randomly generated number
    	int i = 0;//Record the number of guesses
    	while(true) {
    		i = sc.nextInt();//Enter the number of guesses
    		//Judge the guess and prompt according to the situation
    		if(number==i) {
    			System.out.println("Congratulations, you guessed right");
          break;//Jump out of the loop
    		}else if(number>i) {
    			System.out.println("Your guess is too small");
    		}else if(number<i) {
    			System.out.println("Your guess is too big");
    		}
    	}
  }
}

Topics: Java