My third day of learning Java in Shanghai LeByte

01. Operator overview and arithmetic operators

  • Overview

    • Symbols for operating constants and variables
  • classification

Insert picture description here

  • Arithmetic Operator

Insert picture description here

  • Code

    `public class Demo01_Operator {
          
          
    	public static void main(String[] args) {
          
          
    		// +,-,*,/,%
    		int num1 = 4;
    		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);
    	}
    }` 
    
    *   1
    *   2
    *   3
    *   4
    *   5
    *   6
    *   7
    *   8
    *   9
    *   10
    *   11
    *   12
    *   13
    *   14
    *   15
    *   16
    *   17
    
    
    

02. The usage of arithmetic operators ++ and –

  • Code

    `public class Demo02_Operator {
          
          
    	public static void main(String[] args) {
          
          
    		//不管++放在变量的前面还是后面,变量都会增加
    		int num1 = 1;
    		//如果++放在变量的后面,整体等于原值
    		int num2 = num1++;
    		System.out.println("num1 :" + num1);
    		System.out.println("num2 :" + num2);
    		
    		int num3 = 1;
    		//++放在变量的前面,整体等于原值加一
    		int num4 = ++num3;
    		System.out.println("num3 :" + num3);
    		System.out.println("num4 :" + num4);
    		//--放在变量的后面,整体等于原值
    		//--放在变量的前面,整体等于原值减一
    	}
    }` 
    
    *   1
    *   2
    *   3
    *   4
    *   5
    *   6
    *   7
    *   8
    *   9
    *   10
    *   11
    *   12
    *   13
    *   14
    *   15
    *   16
    *   17
    *   18
    
    
    
  • to sum up

    • Whether ++ is placed before or after the variable, the variable will increase
    • ++ is placed after the variable, the whole is equal to the original value
    • ++ is placed in front of the variable, the whole is equal to the original value plus one

03. Arithmetic operators ++ and – exercises

  • Interview questions

    • Which sentence will report an error? why?

    • `byte b = 10;
      b++;  //这里运行后会强制转换为int类型
      b = b + 1; //此句报错,这里需要强制转换` 
      
      *   1
      *   2
      *   3
      
      
      

04. Assignment Operator

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

05. Relational operators

  • classification

    • ==,!=,>,>=,<,<=
  • Code

    `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);
    		}
    }` 
    
    *   1
    *   2
    *   3
    *   4
    *   5
    *   6
    *   7
    *   8
    *   9
    *   10
    *   11
    *   12
    *   13
    *   14
    *   15
    
    
    

06. Bitwise operators

  • classification

    • ^、>>、<<
  • Overview

    `^:相同取0,不同取1
    >>:向右位移,左边空白补0
    <<:向左位移,右边空白补0` 
    
    *   1
    *   2
    *   3
    
    
    

07. Logical operators

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

08. The difference between logical operators & and &&

  • Overview
    • &和&&
      • Same: same effect
      • different
        • &No matter what is in front, it will be executed later
        • && If the front is false, the latter will not be executed
    • |and||
      • Same: same effect
      • different
        • |No matter what is in front, it will be executed later
        • ||If the front is true, the latter will not be executed

09. Ternary Operator

  • grammar

    `条件表达式 ? 条件一 : 条件二;` 
    
    *   1
    
    
    
    • If the conditional expression is true, then execute expression 1
    • If the conditional expression is false, then execute expression 2
    • The ternary operation will have the execution result, which can be received with variables, and the variable type depends on (expression 1 and expression 2)

10. Ternary Operator: Two Tigers

  • demand:

    • There are two Tiggers in the zoo. The weights of the two Tiggers are known to be 180kg and 200kg respectively. Please use the program to determine whether
      the weights of the two Tiggers are the same?
  • Code

    `public class Demo11_Operator11 {
          
          
    	public static void main(String[] args) {
          
          
    		int tiger1 = 180;
    		int tiger2 = 200;
    		String isEquals = tiger1 ==tiger2 ? "相等" : "不相等";
    		System.out.println(isEquals);
    	}
    }` 
    
    *   1
    *   2
    *   3
    *   4
    *   5
    *   6
    *   7
    *   8
    
    
    

11. The height of the three monks

  • demand:
    • There are three monks living in a temple. It is known that their heights are 150cm, 165cm, and 210cm respectively. Please use the program to obtain
      the highest height of these three monks.
  • Code
`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("最高和尚的身高:"+max);
	}
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9


12. Java Expression

  • Overview
    • Formulas composed of variables, constants, and operators
  • classification
    • Assignment expression
    • Arithmetic expression
    • Logical expression
    • Comparison expression
    • Ternary expression

13. Scanner keyboard input

  • Overview

    • In the process of program execution, keyboard input increases interactivity and improves user experience
  • step

    • Guide package
    • Create keyboard entry objects
    • Prompt entry
    • Start typing
  • Code

    `//1,导包
    import java.util.Scanner;
    public class Demo14_Scanner {
          
          
    	
    	public static void main(String[] args) {
          
          
    
    		//2,创建键盘录入对象
    		Scanner scanner = new Scanner(System.in);
            //3,提示录入
    		System.out.println("请录入一个整数:");
    		//4,开始录入
    		int num = scanner.nextInt();
    		System.out.println("num : " + num);
            
    		System.out.println("请录入一个小数:");
    		double num2 = scanner.nextDouble();
    		System.out.println("num2 : " + num2);
    		
    	}
    	
    }` 
    
    *   1
    *   2
    *   3
    *   4
    *   5
    *   6
    *   7
    *   8
    *   9
    *   10
    *   11
    *   12
    *   13
    *   14
    *   15
    *   16
    *   17
    *   18
    *   19
    *   20
    *   21
    
    
    

14. Two Tigers Upgraded Version of Keyboard Input

  • demand

    • There are two Tiggers in the zoo. The weight of the two Tiggers must be measured. Please use the program to determine whether the weights of the two Tiggers are the same.
  • Code

  • `import java.util.Scanner;
    public class Demo15_Scanner2 {
          
          
    
    	public static void main(String[] args) {
          
          
    		//创建键盘录入对象
    		Scanner input = new Scanner(System.in);
    		System.out.println("请输入第一只老虎的体重:");
    		int tiger1 = input.nextInt();
    		System.out.println("请输入第二只老虎的体重:");
    		int tiger2 = input.nextInt();
    		//三目运算符对体重进行比较
    		String isEqueals = tiger1 ==tiger2 ? "相等": "不相等";
    		System.out.println(isEqueals);
    		input.close();
    	}
    }` 
    
    *   1
    *   2
    *   3
    *   4
    *   5
    *   6
    *   7
    *   8
    *   9
    *   10
    *   11
    *   12
    *   13
    *   14
    *   15
    *   16
    
    
    

15. An upgraded version of the height of the three monks using keyboard input

  • demand

    • There are three monks in a temple. Their heights must be measured. Please use the program to obtain the highest heights of these three monks.
  • Code

    `import java.util.Scanner;
    public class Demo16_Scanner3 {
          
          
    
    	public static void main(String[] args) {
          
          
    		//创建键盘录入对象
    		Scanner input = new Scanner(System.in);
    		System.out.println("请输入第一个和尚身高:");
    		int monk1 = input.nextInt();
    		System.out.println("请输入第二个和尚身高:");
    		int monk2 = input.nextInt();
    		System.out.println("请输入第三个和尚身高:");
    		int monk3 = input.nextInt();
    		//三目运算符对身高进行比较
    		int max = (monk1 > monk2 ? monk1 : monk2) > monk3 ? (monk1 > monk2 ? monk1 : monk2) : monk3;
    		System.out.println("最高身高是:"+ max);
    		input.close();
    	}
    }` 
    
    *   1
    *   2
    *   3
    *   4
    *   5
    *   6
    *   7
    *   8
    *   9
    *   10
    *   11
    *   12
    *   13
    *   14
    *   15
    *   16
    *   17
    *   18
    
    
    

16. Flow Control Statement

  • Overview

    • Can control the execution flow of the program
  • classification

    image-20200702170159190

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(条件表达式){
          
          
        语句体;
    }` 
    
    *   1
    *   2
    *   3
    
    
    
  • if double branch syntax

`if(比较表达式){
    
    
    表达式1;
} else {
    
    
    表达式2;
}` 

*   1
*   2
*   3
*   4
*   5


 `input.close();
}` 

*   1
*   2


}

 `### 16. 流程控制语句

* 概述

* 可以控制程序的执行流程

* 分类

[外链图片转存中...(img-PU6ma13n-1593693245083)]





### 17. 选择结构语句之if分支

* 选择结构语句

* if
* switch

* if语句

* if单分支
* if双分支
* if多分支

* if单分支语法

```java
if(条件表达式){
    语句体;
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36


  • if double branch syntax
if(比较表达式){
    
    
    表达式1;
} else {
    
    
    表达式2;
}

Guess you like

Origin blog.csdn.net/dirft_lez/article/details/109193586