Application of java basic statements

do... while statement

Basic format:
do {
Loop body statement;
}While (conditional judgment statement);

Full format:
Initialization statement;
do {
Loop body statement;
Conditional control statement;
}While (conditional judgment statement);

Execution process:
1: Execute initialization statement
2: Execute loop body statement
3: Execute conditional control statements
4: Execute the conditional judgment statement to see whether the result is true or false
If false, the loop ends
If true, continue
5: Go back to 2 and continue
Case demonstration

public class DoWhileDemo {
    public static void main(String[] args) {
        //Requirement: output "HelloWorld" 5 times on the console
		//for loop implementation
		for(int i=1; i<=5; i++) {
			System.out.println("HelloWorld");
		}
		System.out.println("--------");
		
		//do...while loop implementation
		int j = 1;
		do {
			System.out.println("HelloWorld");
			j++;
		}while(j<=5);
		
    }
}

for statement

Format:
For (initialization statement; conditional judgment statement; conditional control statement){
Loop body statement;
}

Execution process:
1: Execute initialization statement
2: Execute the conditional judgment statement to see whether the result is true or false
If false, the loop ends
If true, continue
3: Execute loop body statement
4: Execute conditional control statements
5: Go back to 2 and continue
Case demonstration

public class ForDemo {
    public static void main(String[] args) {
        //Requirement: output "HelloWorld" 5 times on the console
		System.out.println("HelloWorld");
		System.out.println("HelloWorld");
		System.out.println("HelloWorld");
		System.out.println("HelloWorld");
		System.out.println("HelloWorld");
		System.out.println("--------");
		
		//Cycle improvement
		for(int i=1; i<=5; i++) {
			System.out.println("HelloWorld");
		}
		
    }
}

Case 2
/*
	output data
	
	Requirements:
		Outputs 1-5 and 5-1 at the console
*/
public class ForTest01 {
    public static void main(String[] args) {
		//Requirements: output data 1-5
        for(int i=1; i<=5; i++) {
			System.out.println(i);
		}
		System.out.println("--------");
		
		//Requirements: output data 5-1
		for(int i=5; i>=1; i--) {
			System.out.println(i);
		}
		
    }
}
Case 3
/*
	Sum
	
	Requirements:
		Sum the data between 1-5 and output the sum result on the console
*/
public class ForTest02 {
    public static void main(String[] args) {
		//The final result of summation must be saved. You need to define a variable to save the summation result. The initial value is 0
		int sum = 0;
		
		//The data from 1 to 5 is completed by using the circular structure
		for(int i=1; i<=5; i++) {
			//Write the repeated things into the loop structure. The repeated things here are to add the data i to the variable sum used to save the final summation
			sum += i;
			/*
				sum += i;	sum = sum + i;
				The first time: sum = sum + i = 0 + 1 = 1;
				The second time: sum = sum + i = 1 + 2 = 3;
				The third time: sum = sum + i = 3 + 3 = 6;
				The fourth time: sum = sum + i = 6 + 4 = 10;
				The fifth time: sum = sum + i = 10 + 5 = 15;
			*/
		}
		
		//When the loop is completed, the final data is printed out
		System.out.println("1-5 The data and between are:" + sum);
    }
}
Case 4
/*
	Sum of even numbers
	
	Requirements:
		Find the even sum between 1-100, and output the sum result on the console
*/
public class ForTest03 {
    public static void main(String[] args) {
		//The final result of summation must be saved. You need to define a variable to save the summation result. The initial value is 0
		int sum = 0;
		
		//The data summation of 1-100 is almost the same as that of 1-5, except that the end conditions are different
		for(int i=1; i<=100; i++) {
			//For the even number summation of 1-100, it is necessary to add restrictions on the summation operation to judge whether it is an even number
			if(i%2 == 0) {
				sum += i;
			}
		}
		
		//When the loop is completed, the final data is printed out
		System.out.println("1-100 The even sum between is:" + sum);
    }
}

Random

effect:
Used to generate a random number

Use steps:
1: Guide Package
import java.util.Random;
2: Create object
Random r = new Random();
3: Get random number
int number = r.nextInt(10);
The range of data obtained: [0,10) includes 0 and does not include 10

import java.util.Random;

public class RandomDemo {
	public static void main(String[] args) {
		//create object
		Random r = new Random();
		
		//Get 10 random numbers with a loop
		for(int i=0; i<10; i++) {
			//Get random number
			int number = r.nextInt(10);
			System.out.println("number:" + number);
		}
		
		//Requirement: obtain a random number between 1 and 100
		int x = r.nextInt(100) + 1;
		System.out.println(x);
	}
}

Guess the number

Requirements:
	The program automatically generates a 1-100 Between the numbers, use the program to guess what the number is?
	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 data is larger
	If the guessed number is smaller than the real number, it indicates that the guessed data 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.Random;
import java.util.Scanner;

public class RandomTest {
	public static void main(String[] args) {
		//To complete the number guessing game, you first need to have a number to guess, and use a random number to generate the number, ranging from 1 to 100
		Random r = new Random();
		int number = r.nextInt(100) + 1;
		
		while(true) {
			//Use the program to guess numbers. You have to enter the guessed number value every time. You need to use the keyboard to enter it
			Scanner sc = new Scanner(System.in);
			
			System.out.println("Please enter the number you want to guess:");
			int guessNumber = sc.nextInt();
			
			//To compare the input numbers with the system generated data, you need to use branch statements. Use if else.. if.. Format, guess according to different situations, and the result is displayed
			if(guessNumber > number) {
				System.out.println("You guessed the number" + guessNumber + "Big");
			} else if(guessNumber < number) {
				System.out.println("You guessed the number" + guessNumber + "Small");
			} else {
				System.out.println("Congratulations on your guess");
				break;
			}
		}
		
	}
}

while statement

Basic format:
While (conditional judgment statement){
Loop body statement;
}

Full format:
Initialization statement;
While (conditional judgment statement){
Loop body statement;
Conditional control statement;
}
Execution process:
1: Execute initialization statement
2: Execute the conditional judgment statement to see whether the result is true or false
If false, the loop ends
If true, continue
3: Execute loop body statement
4: Execute conditional control statements
5: Go back to 2 and continue

public class WhileDemo {
    public static void main(String[] args) {
        //Requirement: output "HelloWorld" 5 times on the console
		//for loop implementation
		for(int i=1; i<=5; i++) {
			System.out.println("HelloWorld");
		}
		System.out.println("--------");
		
		//while loop implementation
		int j = 1;
		while(j<=5) {
			System.out.println("HelloWorld");
			j++;
		}
		
    }
}

Control jump statement

Continue: used in a loop. Based on condition control, skip the execution of the contents of a loop body and continue the next execution
break: used in a loop to terminate the execution of the contents of the loop body based on condition control, that is, to end the current whole loop

public class ControlDemo {
	public static void main(String[] args) {
		for(int i = 1; i<=5; i++) {
			if(i%2 == 0) {
				//continue;
				break;
			}
			
			System.out.println(i);
		}
	}
}

Keywords: Java Back-end

Added by RSprinkel on Tue, 25 Jan 2022 02:21:43 +0200