Day05 process control statement

Process control statement

Sequential structure word order

/*
Process control statement:
Sequential structure statement: relatively simple
The code is loaded from top to bottom (from the beginning of writing HelloWorld, it is a sequential structure statement)
Select structure statement
Circular structure statement

*/
//The jvm loads this class

class ShunXuDemo{
	
	//main method --- it needs to be called by the jvm, the entry of the program
	public static void main(String[] args){
		
		System.out.println("The program begins,,,,,,") ;
		
		System.out.println("I love Gao Yuanyuan.....") ;
		
		System.out.println("The program is over......") ;
		int i = 100 ;
		System.out.println("over"+i) ;
	}
}

Select structure statement

/*
Select If of structure statement
    
if statement format 1
    
If (conditional expression){
Statement;
            }
            
Execution process
1) if the current conditional expression holds, execute the statement
2) otherwise, the statement will not be executed;
            
The first format of if:
    
Relatively simple: the application scenario is to judge a single condition
*/

//Guide Package
import java.util.Scanner ;
class IfDemo{
	
	public static void main(String[] args){
		
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in) ;
		
		//Prompt and enter data
		System.out.println("Please enter a data x Value of:") ;
		int x = sc.nextInt() ;
		if(x>=10 && x<=20){
			System.out.println("x Between 10 and 20,Value is:"+x) ;
		}
		
		if(x<10){
			System.out.println("x Less than 10,Value is:"+x) ;
		}
		//if()...
		
		System.out.println("over") ;
	}
}

 /*
For if statements, precautions:,
1) semicolons are not allowed where there are left braces after if; Semicolon; Cannot have {left brace where
2) write ---{} local code blocks in the method definition to limit the life cycle of local variables;
Its access scope is {}
        {
            int x = 10 ;
            System.out.println(x) ;
            
        }
*/

import java.util.Scanner ;
class IfDemo2{
	
	public static void main(String[] args){
		
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in) ;
		
		//Prompt and enter data
		System.out.println("Please enter x Value of") ;
		int x = sc.nextInt() ;
		
		//Logical judgment
		
		if(x <=10){
			System.out.println("x Less than or equal to 10,Its value is:"+x) ;
		}
		
		
		//if(x <=10);// If is meaningless, because the semicolon indicates the end of the statement, and there is an empty statement body in if
		
		{ //A local code block in Java that defines the life cycle of a local variable
			System.out.println("x Less than or equal to 10,Its value is:"+x) ;
		}
		
		System.out.println("-----------------------------------") ;
		
		{  //Own scope
			//A local code block in Java that defines the life cycle of a local variable
			int y = 1000  ;
			System.out.println("y:"+y) ;
		}
		//System.out.println(y) ;// Unable to access, no longer within the scope of action
		
		System.out.println("Over....") ;
	}
	
}

/*
if format 2:
        
If (expression){
Statement 1;
        }else{
Statement 2;
        }
        
Execution process:
1) judge whether the expression is true. If it is true, execute statement 1,
2) if not, execute statement 2;
        
Application scenario:
Is to judge two situations;
*/

import java.util.Scanner ; //Guide Package
class IfDemo3{
		
		public static void main(String[] args){
			
			//Create keyboard entry object
			Scanner sc = new Scanner(System.in) ;
			
			//Prompt and enter data
			System.out.println("Please enter a data:") ;
			int num  = sc.nextInt() ;
			
			//if format 2
			if(num>=10){
				System.out.println("num Greater than or equal to 10,Value is"+num) ;
			}else{
				System.out.println("num Less than 10,Value is"+num) ;
			}
			
			System.out.println("Over...") ;
			
			
			System.out.println("----------------------------------------") ;
			
			//Compare the maximum of the two data
			//Prompt and enter data
			System.out.println("Please enter the first data:") ;
			int a  = sc.nextInt() ;
			
			System.out.println("Please enter the second data:") ;
			int b  = sc.nextInt() ;
			
			
			//Define result variables
			int max ;
			if(a>b){
				//System.out.println("max." + a)// Output statement
				max = a ; //Assign a max
			}else{
				max = b ; //Assign b max
			}
			
			System.out.println("The maximum of the two data is:"+max) ;
			
		}
}

/*
Input data with keyboard -- simulate user login operation
The user needs to enter the user name and password - String type, and String type
    
A method provided by the Scanner class:
public String nextLine(): enter String type data
            
            
Requirements:
Simulate user login operation
                
1) assume that the user name and password (simulated database) already exist
                        String name ="admin" ;
                        String pwd = "admin" ;
                
2) enter the user and password with the keyboard
                        username
                        password
                        
3) judgment
                
Function in String type: equals(String other)
Compare whether the contents of two strings are the same
                            
If they are consistent, the entered username and name are consistent, and the content of password is the same as
The content of pwd is consistent, and the prompt, "Congratulations, login succeeded!"
                    
If not, you will be prompted with "sorry, wrong user name or password"
            
            
            
*/ 

//Guide Package
import java.util.Scanner ;
class IfTest{
	public static void main(String[] args){
		
		//String demonstration using keyboard entry
		//Create keyboard entry object
		/*
		Scanner sc = new Scanner(System.in) ;
		
		//Prompt and enter String
		System.out.println("Please enter your user name: ");
		String username = sc.nextLine() ;
		
		System.out.println("username:"+username);
		*/
		
		
		//demand
		//Assume that the user name and password (impersonation database) already exist
		
		String name = "admin" ; //String is a special reference type (described later in common classes)
		String pwd = "admin" ;
		
		//Create a keyboard entry object and prepare to enter user name and password
		Scanner sc = new Scanner(System.in) ;
		
		//Prompt and enter data
		System.out.println("Please enter your user name:") ;
		//public String nextLine(): enter String type data
				//Variable name = fixed format: keyboard entry object nextLine() ;
		String username = sc.nextLine() ;
		
		System.out.println("Please enter a password:") ;
		String password = sc.nextLine() ;
		
		
		//Judge
		//We use the known user name and password to compare with the entered user name and password
		/*
			String Function in type: equals(String other)
			Compare whether the two string contents are the same
		*/
		if(name.equals(username) && pwd.equals(password)){ //agreement
		
			//establish
			System.out.println("Congratulations,Login succeeded!");
			
		}else{
			//Not established
			System.out.println("Contrast,Your user name or password is incorrect!");
		}
		 
		
	}
}


Difference between if statement format 2 and ternary operator
    
        if...else: process control statement. It has a wide range. It can operate not only specific data values, but also statements
        System.out.println("prompt information...);
The 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 - you must be able to use if Else implementation
And if else... Implementation may not be able to use ternary operators!
Later, I saw ternary operators -- almost all in the development documents provided by Jdk -- which involved some
You will see the ternary operator in the original code of the class!
In actual development, business scenario logic judgment - if format 2 is mostly required

Requirements:
1) enter two data on the keyboard and compare the maximum value of the two data
2) enter a data on the keyboard to judge whether the data is even?  

//Guide Package
import java.util.Scanner ;
class IfDemo{
	public static void main(String[] args){
		
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in) ;
		
		//Prompt and enter data
		System.out.println("Please enter the first data:") ;
		int a = sc.nextInt() ;
		
		System.out.println("Please enter the second data:") ;
		int b = sc.nextInt() ;
		
		
		//Method 1: use ternary operators to operate
		int max = (a>b) ? a: b ;
		System.out.println("The maximum of the two data is:"+max) ;
		
		
		System.out.println("------------------------------") ;
		
		//if statement format 2
		int max2 ;//Define result variables
		if(a >b ){
			max2 = a ; 
		}else{
			max2 = b ;
		}
		System.out.println("The maximum of the two data is:"+max2) ;
		
		
		System.out.println("=============================================") ;
		
		//Enter a data on the keyboard to judge whether the data is even
		//Using the if statement
		System.out.println("Please enter a data x:") ;
		int x = sc.nextInt() ;
		
	
		if(x % 2 == 0){
			System.out.println("This data is even") ;
		}else{
			System.out.println("This data is odd") ;
		}
		System.out.println("-----------------------------") ;
		
		
		//Use ternary operator ---- a kind of operator
		// System.out.println("this data is even"); The output statement cannot be the result of a ternary operator
		//FALSE
		//String str = (x%2==0)?(System.out.println("this data is even"):
			//(System.out.println("this data is odd");
		//System.out.println(str) ;
		
		String str = (x % 2==0)? "even numbers":"Odd number" ;
		System.out.println(str) ;
	}
}

/*
        
Format of if statement 3
        
If (expression 1){
Statement 1;
} else if (expression 2){
Statement 2;
} else if (expression 3){
Statement 3;
            ...
            ...
        }else{
Statement 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 true, execute statement 3
            ...
            ...
4) if none of the above conditional expressions holds, execute the statement n.. In else  

Requirements:
Enter a student's grade on the keyboard: (no prompt for data type, int type to receive)
                
90 ~ 100 "excellent"
80-90 "better"
70-80 "good"
60 ~ 70 "pass"
Below 60 "fail"
                

*/

//Guide Package
import java.util.Scanner ;
class IfDemo2{
	
	public static void main(String[] args){
			
			//Create keyboard entry object
			Scanner sc = new Scanner(System.in) ;
			
			//Prompt and enter data
			System.out.println("Please enter the student's grade:") ;
			int socre = sc.nextInt() ;
			
			//Judge according to the above requirement format	
			/*
				The final developer of the program ---- what the "software" user wants to use
				When we test ourselves, we should test the data of this program
						1)Error data
							Negative numbers or data beyond the range are "illegal data"
						2)Correct data
								Data in scope
						3)Boundary data
								critical value
			
			*/
			
			if(socre>100 || socre<0){
				//Error data prompt
				System.out.println("I'm sorry,invalid data!") ;
			}else if(socre>=90 && socre<=100){
				System.out.println("Excellent results") ;
			}else if(socre>=80 && socre<90){
				System.out.println("Good results") ;
			}else if(socre>=70 && socre<80){
				System.out.println("Good results") ;
			}else if(socre >=60 && socre<70){
				System.out.println("Pass the grade") ;
			}else{
				System.out.println("fail,") ;
			}
	}
}

/*
Nesting of if format 2
        
If (expression) {/ / a > b
If (expression 1) {/ / a > C
Statement 1;
            }else{
Statement 2;
            }
        }else{ 
            
If (expression 2) {/ / b > C
Statement 3;
            }else{
Statement 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

Requirement: enter three data on the keyboard to find the maximum value of the three data
        
Method 1: intermediate variable method --- use ternary operator to realize twice
Method 2: directly use the nesting of if format 2
    
*/

//Guide Package
import java.util.Scanner ;
class IfTest{
		
		public static void main(String[] args){
			
			//Create keyboard entry object
			Scanner sc = new Scanner(System.in) ;
			
			//Prompt and enter data
			System.out.println("Please enter the first data:") ;
			int x = sc.nextInt() ;
			
			System.out.println("Please enter the second data:") ;
			int y = sc.nextInt() ;
			
			
			System.out.println("Please enter the third data:")  ;
			int z = sc.nextInt() ;
			
			
			//Method 1): the method of intermediate variable --- use ternary operator to realize it twice
			int temp = (x>y)? x: y ;
			int max = (temp > z)? temp: z ;
			System.out.println("The maximum of the three data is:"+max) ;
			
			System.out.println("-------------------------------") ;
			
			//Mode 2: nesting mode of if format 2
			/*
				
		if((expression){
			if(Expression 1){
				Statement 1;
			}else{
				Statement 2;
			}
		}else{ 
			
			if(Expression 2){
				Statement 3;
			}else{
				Statement 4;
			}
		}
		
		//
			
			*/
			//Define a result variable
			int max2 ;
			if(x>y){
				//If x is greater than y, it is true
				//Continue to judge: using if Else: use x and z for comparison
				if(x>z){
					max2 = x ; 
				}else{
					max2 = z ;
				}
				
			}else{
				//If x is greater than y, it does not hold
				//Continue to judge, using if Else: use y and z for comparison
				if(y>z){
					max2 = y ;
				}else{
					max2 = z ;
				}
			}
			
			System.out.println("The maximum of the three data is:"+max2) ;
		}
}

/*
Requirements:
        
Enter the value of a month on the keyboard to judge the season (regardless of the special month value)
3,4,5 spring
6,7,8 summer
9, 10, 11 autumn
12,1,2 winter
                    
                    
/ / use if format 3 to operate
/ / consider error data, correct data, and boundary data
*/

//Guide Package
import java.util.Scanner ;
class IfTetst2{
	public static void main(String[] args){
		
		//Create keyboard entry object
		Scanner sc  = new Scanner(System.in) ;
		
		//Prompt and enter
		System.out.println("Please enter a value for the month:") ;
		int month = sc.nextInt() ;
		
		
		//Use if format 3 to operate
		if(month >12 || month <= 0){
			System.out.println("I'm sorry,The earth has no such month!") ;
		}else if(month>=3 && month<=5){
			System.out.println("It's spring") ;
		}else if(month>=6 && month<=8){
			System.out.println("It's summer") ;
		}else if(month>= 9 && month <=11){
			System.out.println("It's autumn") ;
		}else{
			System.out.println("winter!") ;
		}
	}
}

/*
Select the switch statement of the structure statement
    
Format
Switch (expression){
case value 1:
Statement 1;
                break ;
case value 2:
Statement 2;
                break ;
            ...
            ...
            ...
            default:
Statement n;
                break ;
            }
            
Execution process:
1) judge whether the value of the expression matches the case value 1,
If the match is successful, execute statement 1 and break ends
2) if the case value 1 does not match, continue to compare it with the value 2 after the case. If it matches, statement 2 executes and break ends;
            ...
            
            ...
3) if the value of the above case does not match the result of the expression in switch, the final
Execute the statement n in default, and then the default execution of the program ends at the end and break ends;  

Requirements: use switch first
Enter a value week on the keyboard to output the "day of the week"
        
week is 1, which is "Monday"
2 "Tuesday"
3 "Wednesday"
                    ...
                    ...
7 "Sunday"

*/

//Guide Package
import java.util.Scanner ;
class SwitchDemo{
	public static void main(String[] args){
		
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in) ;
		
		//Prompt and enter data
		System.out.println("Please enter a data week:") ;
		int week = sc.nextInt() ;
		
		/*
			switch((expression){
			case Value 1: (constant)
				Statement 1;
				break ;
			case Value 2:
				Statement 2;
				break ;
			...
			...
			...
			default:
				Statement n;
				break ;
			}
			
		*/
		switch(week){
		case 1:
			System.out.println("Monday") ;
			break ;
		case 2:
			System.out.println("Tuesday") ;
			break ;
		case 3:
			System.out.println("Wednesday") ;
			break ;
		case 4:
			System.out.println("Thursday") ;
			break ;
		case 5:
			System.out.println("Friday") ;
			break ;
		case 6:
			System.out.println("Saturday") ;
			break ;
		case 7:
			System.out.println("Sunday") ;
			break ;
		default :
			System.out.println("I'm sorry,invalid data!") ;
			break ;
		}
		
		
	}
}

Interview questions:
What data type can an expression in switch be?
Basic data types that can be used:
            byte,short,char,int
After JDK5, you can follow enum
After JDK7, it can be of String type

Precautions in switch statement:
            
The value after the constant 1) statement can only be
(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 a constant
, can also be a variable;)
            
2) the break in the case statement must be carried, otherwise a phenomenon will occur: case penetration
Since a case has been matched, if there is no break at this time, continue to the following
Case statements are penetrated and executed in turn. If there is a break in a case, the switch ends!
            
3) what is the end condition of the switch?
            
a) end of statement break
b) the program is executed to the end by default!
                
                
        
4) the default statement can be anywhere in the statement without affecting the execution process of the program!
            
Execute default. If there is no match in the current case, execute default

/*
Look at the program and write the results
*/
class SwitchTest{
	
	public static void main(String[] args){
		
		
		int x = 3 ;
		int y = 4 ;
		
		/*
		switch(x){//3
		default :
			y++ ;//4+1 = 5 
		case 4:
			y ++ ;//5+1 = 6 
		case 5 :	
			y++ ;//6+1 = 7
		}
		System.out.println("y:"+y) ;
		*/
		
		switch(x){//x=3
		default :
			y++ ;
		case 3: //matching
			y ++ ; //y=4+1 = 5
		case 4 :	
			y++ ;//y=5+1 = 6
		}
		
		System.out.println("y:"+y) ;
		
		System.out.println("------------------------") ;
		int a = 3 ;
		int b = 4 ;
		/*
		switch(a){
			default :
				b++ ;
				break ;
			case 4:
				b++ ;
			case 5: 
				b++;
		}
		*/
		
		switch(a){//a=3
		
			case 3:
				b++ ;
			case 5: 
				b++;
			default :
				b++ ;
				break ;
		}
		System.out.println("b:"+b) ;
		
	}
}

Keywords: Java JavaEE

Added by brmcdani on Wed, 05 Jan 2022 10:44:07 +0200