Second summary

Posted by psr540 on Tue, 04 Jan 2022 20:36:45 +0100

if statement

Format 1:
if(Relational expression){
   Statement body;
}

Execution process:

1. First calculate the value of the relationship expression

2. Execute the statement body if the value of the relationship expression is true

3. If the value of the relationship expression is false, the statement body will not be executed

4. Continue to execute the following statements

Format 2:
if(Relational expression){
    Statement body 1;
}else{
    Statement body 2;
}

Execution process:

1. First calculate the value of the relationship expression

2. If the value of the relationship expression is true, execute statement body 1

3. If the value of the relationship expression is false, execute statement body 2

4. Continue to execute the following statements

if Nesting of format 2
if(Relational expression){// a >b 
	if(Relationship expression 1){ //a>c
		Statement body 1;
	 }else{
		Statement body 2;
	}
}else{ 
			
	if(Relational expression 2){//b>c
		Statement body 3;
	}else{
		Statement body 4;
	}
}

Execution process:

1. First judge whether the expression is true. If it is true

2. Execute the if statement inside

2.1. Judge whether expression 1 is true. If it is true, execute statement 1

2.2 if not, execute statement 2

3. If the comparison expression is not valid for the first time, execute the if statement in else

3.1. Judge whether expression 2 is true. If it is true, execute statement 3

3.2. Otherwise, execute statement 4

Format 3
if(Relationship expression 1){
	Statement body 1;
}else if(Relational expression 2){
	Statement body 2;
}else if(Relational expression 3){
	Statement body 3;
	...
	...
}else{
	Statement body n ;
}

Execution process:
1) Judge whether expression 1 is true. If it is true, execute statement 1;

2) If not, continue to judge whether expression 2 is true. If true, execute statement 2;

3) If expression 2 is not true, continue to judge whether expression 3 is true. If it is true, execute statement 3

​ ...

​ ...

4) If none of the above conditional expressions holds, execute the statement n in else

Difference between if statement format 2 and ternary operator

if... else: process control statement, which has a wide range. It can operate not only specific data values, but also statements

System.out.println("prompt information...);

Ternary operator is a kind of operator. It can only operate on specific data result values and cannot be nested directly

System.out.println("prompt information...);

Ternary operators can be implemented - they must be implemented using if... else

The if... else... Implementation may not be implemented using ternary operators.

Switch statement

Format:
switch(expression){
   case Value 1:
       Statement body 1;
       break;
   case Value 2:
       Statement body 2;
       break;
       ......
   default:  
       Statement body n+1;
       break;
}

Format Description:

Expression: the values are byte, int, short, char. After JDK5, it can be enum, and after JDK, it can be string.

case: followed by the value to be compared with the expression.

break: it means interrupt and end. It is used to end the Switch statement.

default: indicates that the content is executed when all conditions do not match, which is similar to else of if statement.

Precautions in switch statement:

1. The value after the case statement can only be a constant

(in the Java language, Java is a strongly typed language: structure and format are very rigorous)

(in the JavaScript front-end language, it is a weakly typed language, and the value after case can be either a constant or a variable;)

2. The break in the case statement must be carried, otherwise it will cause a phenomenon: case penetration

Since a case has been matched, if there is no break at this time, continue to the following

The case statements are penetrated and executed in turn. If there is a break in the case, the switch ends!

3) What is the end condition of switch?

a. End of statement break

b. The program executes to the end by default!

for loop 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, the loop continues

3. Execute loop body statement

4. Execute conditional control statement

5. Go back to step and continue

for cycle daffodils

class ShuiXianHua{
	public static void main(String[] args){
		System.out.println("How many daffodils are there");
		//Find the number of all daffodils
		for(int i=100;i<=999;i++){
			//To obtain the data of x, you need to determine the data itself on each bit
			//Define three variables, Ge, Shi and Bai 
			int ge=i%10;//Get data on bits
			int shi=i/10%10;//Get ten digit data
			int bai=i/10/10%10;//Get data in hundreds
			//Meet the conditions
			if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i){
				System.out.println(i);
			}
			
			
		}
	}
}

while loop statement

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 statement

5. Go back to step 2 to continue

The difference between a while loop and a for loop

Common ground: they are all circular statements, which optimize the code with high repetition and solve the problem of redundancy

difference:

1. Different formats

For (initialization statement; conditional expression; control body statement){

Loop body statement;

​ }

Initialization statement;

While (conditional expression){

Loop body statement;

Control body statement;

​ }

2. From the perspective of memory, after the for loop is used, the current variable will be released, while while it will not. It will always be used

You can access this variable, so relatively speaking, the for loop saves memory space!

3) Number of release cycles

Specify the number of cycles: give priority to the for loop, followed by the while loop

while loop: it is not clear how many cycles to use

do-while Statements

Format:
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 statement

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 step 2 to continue

The biggest difference between the do while loop and the other two loops: the loop body is executed at least once;

loop nesting

Statement structure:
Sequential statement: ending with a semicolon indicates the end of a sentence
 Branch statement: a pair of curly braces if The overall structure, the overall description of a complete if sentence
        A bunch of braces indicate switch The overall structure, the overall description of a complete switch sentence
 Circular statement: a pair of curly braces for The overall structure, the overall description of a complete for sentence
		A pair of braces indicates while The overall structure, the overall description of a complete while sentence
		do..while End with a semicolon and describe a complete picture as a whole do..while sentence

Role and steps of Random

Function: generate a random number

Use steps:

1. Guide Package

import java.util.Random;

2. Create object

Random r=new Random;Number of
r Yes, the variable name can be changed, and others are not allowed to be changed

3. Get random number

int number=r.nextInt();//Randomly obtain a number in the range [0.0,1.0].

Jump control statement: break, continue, return

Break: break, end.

break application scenario:

1. It cannot be used alone and an error is reported

2. It can only be used in two scenarios

Continue: it is a kind of jump control statement, which is generally used in loop statements. Indicates to end the current cycle and immediately enter the next cycle.

return: returns the result (the end method is used, but rarely used alone)

method

Methods with specific return value types

Method: it is a code set that organizes code blocks with independent functions into a whole and makes them have special functions.

Format: fixed writing

Definition method	

Permission modifier static  Return value type method name(Parameter type 1 variable name 1 ,Parameter type 2 variable name 2.....){

Complete the business logic of your code:Complete the operation according to the requirements

return result;

}

Permission modifier: now use public fixedly: the access permission is large enough

Return value type: data type

Method name: name the {} code block, follow the "identifier rule" and the small hump naming method

Parameter list:

Parameter type: data type. Now the basic data type is used

Variable name -----: name the current formal parameter, "small hump naming method"

Return result: according to your business method and the type of return value, you will be returned with specific results.

Assignment calls are recommended in methods with specific return value types

The definition method is that there must be two clear points in mind

1. Specify the return value type (data type): use the basic data type int

2. Specify the parameter type and number

Method with no specific return value type

Format:
public static void Method name(Formal parameter list){
				//Completed business logic, output and other operations,
				But no return (Must be used with methods with specific return value types)
			}

It is recommended to call a method without a specific return value type

Method overloading

Method overloading: multiple methods are in the same class

Multiple methods have the same method name

Multiple methods have different parameters, different types or different quantities

array

Array: a container that can store the same data type

Format 1: data type[] Variable name
 example: int[] arr
 Defines a int The array name is arr
 Format 2: data type variable name[]
example: int arr[]
Defines a int Variable of type, variable name is arr

There are two ways to initialize an array:

The first is dynamic initialization

The second is static initialization

Dynamic initialization: only the array length is specified during initialization, and the system allocates the initial value for the array.
Formats: data types[] Variable name=new data type[Array length];
example: int[] arr=new int[3];
Static initialization: specify the initial value of each array element during initialization, and the array length is determined by the system
 Formats: data types[] Variable name=new data type[]{Data 1, data 2, data 3,...};
example: int[] arr=new int[]{1,2,3};
Simplified formats: data types[] Variable name={Data 1, data 2, data 3,...};
example: int[] arr={1,2,3};

Common exceptions in arrays: runtime exceptions

1. Array subscript out of bounds exception - an exception occurs when the program is running

ArrayIndeexOutOfBoundsException class name

The reason for this exception: an error is reported when we access the index value that does not exist in the array.

Solution: change the index value

2. Null pointer exception NullPointerException

At present, when we access an object, the object is already null, and then we have to use a function in the object - a null pointer appears

Solution: judge whether the current object is not empty

Gets the number of array elements

length:You can quickly get the array length and array name.length
for(int x = 0 ; x < arr.length ; x ++){//Maximum subscript value = length of array - 1;
			System.out.println(arr[x]) ;
		}

Bubble sorting

Bubble sorting: compare two by two. The larger value will be used later. After the first comparison, the maximum value will appear
At the largest index, compare in this order.

import java.util.Scanner;
class MaoPao{
	public static void main(String[] args){
		int[] arr=new int[6];
		Scanner sc=new Scanner(System.in);
        //Use the for loop to prompt for elements
		for(int n=0;n<arr.length;n++) {
			System.out.println("Please enter page"+(n+1)+"Elements:");
			arr[n]=sc.nextInt();
		}
		sort(arr);
		printArr(arr);
		System.out.println("The sorted array is");
		printArr(arr);
	}
    //Bubble sort using method
	public static void sort(int[] arr){
		for(int x = 0 ; x < arr.length-1 ; x ++){//Comparison times
            //Judge and compare the elements inside
			for(int y = 0 ; y < arr.length-1-x ; y ++){
                //The front elements are larger than the back elements and are interchangeable
				if(arr[y] > arr[y+1]){
                    //Intermediate variable exchange
					int temp = arr[y] ;
					arr[y] = arr[y+1] ;
					arr[y+1] = temp ;
				}
			}
    //Define the method of traversal
	public static void printArr(int[] arr){
        //Output left bracket
		System.out.print("[") ;
        //Traversal array
		for(int x = 0 ; x < arr.length ; x ++){
			if(x == arr.length-1){
				System.out.println(arr[x]+"]") ;
			}else{
				System.out.print(arr[x]+", ") ;
			}
		}
	}
}

Topics: JavaEE