The third day of learning java

01. operator overview and arithmetic operators

  • summary

    • Symbols that operate on constants and variables
  • classification

  • Arithmetic operator

  • code implementation

    public class Demo01_Operator {
    	public static void main(String[] args) {
    		// +,-,*,/,%
    		int num1 = 4;
    		int num2 = 2;
    		//Addition
    		System.out.println(num1+num2);
    		//subtract 
    		System.out.println(num1-num2);
    		//Multiply
    		System.out.println(num1*num2);
    		//be divided by
    		System.out.println(num1/num2);
    		//Residual
    		System.out.println(num1%num2);
    	}
    }
    

02. usage of arithmetic operators ++ and -

  • code implementation

    public class Demo02_Operator {
    	public static void main(String[] args) {
    		//Whether ++ is placed before or after a variable, the variable will increase
    		int num1 = 1;
    		//If ++ is placed after the variable, the whole is equal to the original value
    		int num2 = num1++;
    		System.out.println("num1 :" + num1);
    		System.out.println("num2 :" + num2);
    		
    		int num3 = 1;
    		//++Before the variable, the whole is equal to the original value plus one
    		int num4 = ++num3;
    		System.out.println("num3 :" + num3);
    		System.out.println("num4 :" + num4);
    		//--After the variable, the whole is equal to the original value
    		//--Before the variable, the whole is equal to the original value minus one
    	}
    }
    
  • summary

    • Whether ++ is placed before or after a variable, the variable will increase
    • ++After the variable, the whole is equal to the original value
    • ++Before the variable, the whole is equal to the original value plus one

03. arithmetic operators ++ and - Exercise

  • Interview questions

    • Which sentence is wrong? Why?

    • byte b = 10;
      b++;  //Here, it will be cast to int type after running
      b = b + 1; //An error is reported in this sentence, and a forced conversion is required here
      

04. assignment operator

  • classification
    • Basic assignment operator
      • =
    • Advanced assignment operators
      • +=,-=,/=,%=,*=

05. relational operators

  • classification

    • ==,!=,>,>=,<,<=
  • code implementation

    public class Demo05_Operator5 {
    	public static void main(String[] args) {
    		//==,!=,>,>=,<,<=,
    		int num1 = 1;
    		int num2 = 2;
    		System.out.println(num1==num2);
    		System.out.println(num1!=num2);
    		System.out.println(num1>num2);
    		System.out.println(num1>=num2);
    		System.out.println(num1<num2);
    		System.out.println(num1<=num2);
    		double num3 = 2.0000;
    		System.out.println(num3==num2);
    		}
    }
    

06. bitwise operator

  • classification

    • ^,>>,<<
  • summary

    ^:0 for the same, 1 for the different
    >>:Shift to the right, fill 0 in the left blank
    <<:Shift to the left, fill 0 in the blank on the right
    

07. logical operators

  • classification
    • &
      • As long as one is false, all are false
    • |
      • As long as one is true, all are true
    • !
      • True or false, false or true

08. differences between logical operators & and & &

  • summary
    • &And&&
      • Same: same execution effect
      • Different
        • &No matter what the front is, it will be executed later
        • &&If the preceding is false, the following will not be executed
    • |And||
      • Same: same execution effect
      • Different
        • |No matter what the front is, it will be executed later
        • ||If the previous value is true, it will not be executed later

09. ternary operator

  • grammar

    Conditional expression ? Condition 1 : Condition II;
    
    • If the conditional expression is true, the expression 1 is executed
    • If the conditional expression is false, execute expression 2
    • The ternary operation will have execution results and can be received with variables. The type of variables depends on (expression 1 and expression 2)

10. two tigers of trinocular operator

  • Requirements:

    • There are two tiggers in the zoo. It is known that the two tiggers weigh 180kg and 200kg respectively. Please use the program to judge the two tiggers
      Are you the same weight?
  • code implementation

    public class Demo11_Operator11 {
    	public static void main(String[] args) {
    		int tiger1 = 180;
    		int tiger2 = 200;
    		String isEquals = tiger1 ==tiger2 ? "equal" : "Unequal";
    		System.out.println(isEquals);
    	}
    }
    

11. height of the three monks of the three eye operator

  • Requirements:
    • There are three monks living in a temple. Their heights are known to be 150cm, 165cm and 210cm respectively. Please use the program to obtain
      Take the highest height of the three monks.
  • code implementation
public class Demo12_Operator12 {
	public static void main(String[] args) {
		int monk1 = 150;
		int monk2 = 165;
		int monk3 = 210;
		int max =(monk1>monk2?monk1:monk2)>monk3?(monk1>monk2?monk1:monk2):monk3;
		System.out.println("Height of the highest monk:"+max);
	}
}

12. java expression

  • summary
    • An expression consisting of variables, constants, and operators
  • classification
    • Assignment expression
    • Arithmetic expression
    • Logical expression
    • Compare expressions
    • Ternary expression

13. Scanner keyboard input

  • summary

    • In the process of program execution, keyboard input can increase interactivity and improve user experience
  • step

    • Guide Package
    • Create a keyboard entry object
    • Prompt entry
    • Start entry
  • code implementation

    //1. Guide Package
    import java.util.Scanner;
    public class Demo14_Scanner {
    	
    	public static void main(String[] args) {
    
    		//2. Create a keyboard entry object
    		Scanner scanner = new Scanner(System.in);
            //3. Prompt for entry
    		System.out.println("Please enter an integer:");
    		//4. Start entry
    		int num = scanner.nextInt();
    		System.out.println("num : " + num);
            
    		System.out.println("Please enter a decimal:");
    		double num2 = scanner.nextDouble();
    		System.out.println("num2 : " + num2);
    		
    	}
    	
    }
    

14. upgraded version of two tigers for keyboard input

  • demand

    • The weight of two tiggers in the zoo must be measured. Please use the program to judge whether the weight of two tiggers is the same
  • code implementation

  • import java.util.Scanner;
    public class Demo15_Scanner2 {
    
    	public static void main(String[] args) {
    		//Create a keyboard entry object
    		Scanner input = new Scanner(System.in);
    		System.out.println("Please enter the weight of the first tiger:");
    		int tiger1 = input.nextInt();
    		System.out.println("Please enter the weight of the second Tiger:");
    		int tiger2 = input.nextInt();
    		//Comparison of body weight by trinocular operator
    		String isEqueals = tiger1 ==tiger2 ? "equal": "Unequal";
    		System.out.println(isEqueals);
    		input.close();
    	}
    }
    

15. keyboard input of the upgraded version of three monks' height

  • demand

    • There are three monks in a temple. Their height must be measured. Please use the program to obtain the maximum height of the three monks.
  • code implementation

    import java.util.Scanner;
    public class Demo16_Scanner3 {
    
    	public static void main(String[] args) {
    		//Create a keyboard entry object
    		Scanner input = new Scanner(System.in);
    		System.out.println("Please enter the height of the first monk:");
    		int monk1 = input.nextInt();
    		System.out.println("Please enter the height of the second monk:");
    		int monk2 = input.nextInt();
    		System.out.println("Please enter the height of the third monk:");
    		int monk3 = input.nextInt();
    		//Three eye operator for height comparison
    		int max = (monk1 > monk2 ? monk1 : monk2) > monk3 ? (monk1 > monk2 ? monk1 : monk2) : monk3;
    		System.out.println("The maximum height is:"+ max);
    		input.close();
    	}
    }
    

16. process control statement

  • summary

    • Can control the execution process of the program
  • classification

17. select if branch of structure statement

  • Select structure statement

    • if
    • switch
  • if statement

    • if single branch
    • if double branch
    • if multi branch
  • if single branch syntax

    if(Conditional expression){
        Statement body;
    }
    
  • if double branch syntax

if(Compare expressions){
    Expression 1;
} else {
    Expression 2;
}
	input.close();
}

}



### 16. process control statement

* summary

* Can control the execution process of the program

* classification

[External chain picture transferring...(img-PU6ma13n-1593693245083)]





### 17. select if branch of structure statement

* Select structure statement

* if
* switch

* if statement

* if Single branch
* if Double branch
* if Multi branch

* if Single branch grammar

```java
if(Conditional expression){
    Statement body;
}
  • if double branch syntax
if(Compare expressions){
    Expression 1;
} else {
    Expression 2;
}

Tags: Java

Posted by nielsg on Wed, 01 Jun 2022 04:26:14 +0530