day2-java data type conversion, common operators

day02——data type, operator

I believe that after studying in the previous section, my friends have successfully installed the Java operating environment and mainstream development tools, and have a certain understanding of Java. Next, let us continue to learn new knowledge about Java. Come on, come on! Awesome! ! ! ! ! ! !
Previous section article addressJava learning-getting started (Java introduction, jdk download and installation and environment variable configuration, IDEA download and installation)

1. Detailed explanation of data representation

Yesterday we learned about variables. We know that variables can be used to record data. So in what form is data represented at the bottom of the computer? Next we will learn the underlying principles of data in computers.

1.1 The storage principle of integers in computers

In fact, any data in the computer is represented bybinary. Then someone here must ask, what is binary? The so-called binary is actually a representation of data, and its characteristic is that every 2 is entered into 1.

In addition to binary (every 2 counts as 1), octal (every 8 counts as 1), there are also decimal (every 10 counts as 1), hexadecimal (every 10 counts as 1), etc.

For most binary students, they should be very unfamiliar. It doesn't matter! Come on, follow my ideas, and you will know how binary represents data.

1.二进制中只有01两个数
	首先十进制的0和二进制的0是一样的,十进制的1和二进制的1也是 一样的。但是十进制中	  有2,但是二进制中就没有22.那么二进制是如何表示十进制的2呢?
	1
+	1		
——————————	
   10	  这里两个1相加,结果为2,由于二进制满21,所以最终结果10
   
3.那么二进制是如何表示十进制的3呢?
	前面我们已经知道二进制10表示十进制的2,那么二进制10+1就表示十进制的3啊!
	10
+    1
—————————
    11	 十进制的3对应的二进制是11

4.那么二进制是如何表示十进制4的呢?
	前面我们已经知道二进制11表示十进制的4,那么11+1就表示十进制的511
+    1
—————————
   100   十进制的5对应的二进制是100

你找到规律了吗? 你能不能依次写出5的二进制、6的二进制?

Each binary data calculated above is calculated one by one by adding +1 to every 2. Is there a faster way to calculate the decimal equivalent of binary? Here is a method to learn: it is called division by 2 and remainder method.

  • Divide by 2 and take remainder
1.怎么做呢?
	答:让十进制数据连续除以2,直到商为0,余数反转

2.举例1:把十进制6转换为二进制	
			商	余数
	6/2     3    0
    3/2		1	 1
    1/2		0	 1
    然后把余数反转:6对应的二进制是110
    
3.举例2: 把十进制13转换为二进制
			商	余数
	13/2	6	 1
    6/2		3	 0
    3/2	 	1 	 1
    1/2		0	 1
    然后把余数反转:10对应的二进制是1101
    
4.练习1:你能把十进制7转换为二进制吗?
	自己试试吧!

We will learn here first about how the data recorded by variables is represented in the computer.

  • The smallest storage unit of a computer

We already know that computers use binary to represent data. Here I want to ask another question! I now want to store an integer 6 in the computer. When converted to binary, it is 110. So does the computer only store 110 as a three-digit number? Actually not, The smallest storage unit in a computer is a byte, and a byte occupies 8 bits (bit), that is to say, even if this Even if the data is less than 8 bits, it needs to be stored in 8 bits.
Insert image description here

We find a file at random and look at the properties of the file. We can see that the size of the file is in bytes.

Insert image description here

1.2 Principles of character storage in computers

Through the study in the previous section, we know how integers are stored in computers? So how are characters stored in computers?

In fact, characters are not stored directly, but each character is encoded into an integer, and the binary form of the character corresponding to the integer is stored. Americans have developed a table of correspondences between characters and integers, called the ASCII encoding table.

ASCII编码表中字符编码的规律:
	1.字符0对应48,后面的1,2,3,4...9 对应的十进制整数依次往后顺延
	2.字符a对应97,后面的b,c,d,e...z 对应的十进制整数依次往后顺延
	3.字符A对应65,后面的B,C,D,E...Z 对应的十进制整数依次往后顺延

Insert image description here

It should be noted that Chinese characters are not included in the ASCII encoding table. Chinese characters are in other coding tables, which we will introduce separately later. That's all you need to know about the storage of characters in computers.

1.3 Storage principle of pictures, videos and sounds

  • Picture storage

Through the above study, we already know how integers and characters are stored, and they will eventually be converted into binary data, right! How are pictures, sounds, and videos stored? Let’s also find out

Let's start with pictures. If you keep zooming in on a picture, you will see a mosaic effect. You will find that every detail in the picture is composed of small squares, and each small square is actually a color. Any color can be represented by three primary colors, referred to as RGB, among which R (red), G (green), B (blue), and each color in RGB is represented by a one-byte integer, the minimum value is The maximum value of 0 is 255

Insert image description here

RGB(0,0,0)表示黑色
RGB(255,255,255)表示白色
RGB(255,0,0) 表示红色
RGB(255,255,0) 表示红色和绿色混合为黄色
RGB(255,0,255) 表示红色和蓝色混合为紫色
...

You can adjust any color by specifying RGB values ​​in the color editor of the drawing board. A picture is actually composed of many small square colors, and each color is represented by an integer of the three primary colors of RGB. The integer will eventually be converted into binary for storage.

Insert image description here

  • Video storage

In fact, videos and pictures are the same. If you play multiple pictures continuously, more than 24 pictures in one second, due to the phenomenon of visual persistence of the human eye, the human eye cannot feel the time interval between picture switching, so it is considered to be Continuous video.

  • sound storage

Students who have studied physics must know that sound propagates in the form of waves. We can represent sound waves on a coordinate system, then pick some points on the coordinate system, and store the coordinate values ​​of these points into the computer in binary form. This is the storage principle of sound.

Insert image description here

1.4 Other representations of data

  • binary to decimal conversion

Earlier we learned that decimal can be converted to binary using the division by 2 method, so can we conversely convert binary to decimal?

Here is a calculation method called:8421 code

In order to facilitate understanding, let's first look at how to convert decimal to decimal, mainly to let everyone see the evolution process.

1.十进制转十进制
	比如我们把12345进行分解:
        12345 = 10000 + 2000 + 300 + 40 + 5
              = 1*10^4 + 2*10^3 + 3*10^2 + 5*10^0
	我们发现:
		在十进制中如果把十进制的每一位从右往左从0开始编一个号,假设这一位数字是a,			那么这一位数表示的值就是:a*10^n;
	
----------------------------------------------------------------------2.二进制转十进制:
	类比十进制:
		如果把二进制的每一位从从右往左0开始编一个号用n表示,假设二进制的每一位是a,
		那么这一位表示的十进制值是:a*2^n
		
	1)假设二进制的每一位都是1128	64	32	16	8	4	2	1	每一位表示的十进制:a*2^n
		7	6	5	4	3	2	1	0	编号:n
		1	1	1	1	1	1	1	1   二进制的每一位:a
	
        二进制		 十进制
        11111111 = 1*2^7  + 1*2^6 + 1*2^5 + ... + 1*2^0
                 = 128    + 64    + 32    + ... + 1
                 = 255
    
    2)假设二进制的为0010001
    	128	64	32	16	8	4	2	1	每一位表示的十进制:a*2^n
    	7	6	5	4	3	2	1	0	编号:n
    	0	0	1	0	0	0	0	1	二进制的每一位:a
    
    	二进制				十进制
    	00001101 = 0*2^7 + 0*2^6 + 1*2^5 + ... + 1*2^0
    			 = 0     + 0     + 32 	 + ... + 1
    			 = 33

    3)8421码:从右往左给二进制的每一位数依次按照1 2 4 8...标记
    	128	64	32	16	8	4	2	1
    	0	0	1	0	0	0	0	1
    	-----------------------------
    	只需要将1位上的数字加起来,就是二进制对应的十进制
    	
    	二进制			  十进制
    	00001101 = 		8+4+1
    			 = 		13
    	
    	0000111	 =      4+2+1
    			 = 		7
    	
    	0001111  =      8+4+2+1
    			 =      25
  • Binary to octal

Earlier we said that data in computers are stored in binary, but binary is inconvenient to read and write. In order to facilitate reading and writing, octal and hexadecimal systems were introduced.

1.运算规则:
	把二进制的每三位一组合,然后对每三位用8421码进行计算,最后拼接到一起
	原因:因为111,的值是7, 再大满7就需要往进位了。 
	
2.把二进制11001110转换为八进制数据
	01	100 001		二进制每三位一组合
    1	4	1		每一组进行8421码运算
    ----------
    八进制:141
  • Binary to hexadecimal
1.运算规则:
	把二进制的每四位一组合,然后对每四位用8421码进行计算,最后拼接到一起
	原因:因为1111,的值是15, 再大116了就需要往进位了。 
	
2.举例:把二进制11001110转换为十六进制数据
	0110 0001		二进制每四位一组合
    6	 1			每一组进行8421码运算
    ----------
    十六进制:61
    
3.练习:把111100转换为十六进制
	0011 1100
    3	 12		由于十六进制中有a,b,c,d,e,f分别表示10,11,12,13,14,15
    -----------
    十六进制:3c

1.5 Writing formats of different bases in Java programs

System.out.pirntln('a'+1); //98
System.out.pirntln(0b01100001); //97
System.out.pirntln(0141); //97
System.out.pirntln(0x61); //97

2. Detailed explanation of data types

In the previous course, we have learned the definition of variables. When defining variables, we need to declare the data type. The data type here is used to specify variable storage. What type of data.

For exampleint a = 10; Hereint restricts variables to only store integers; in addition to the int data type, Java also provides many other data types. Java's data types are generally divided into two categories: Basic data types, Reference data types.

Today we mainly study basic data types. There are 4 categories and 8 basic data types. Each data type occupies different memory space and can represent different data ranges. As shown below

Insert image description here

What we need to pay attention to is that if you write an integer or decimal literal, it also has a default data type.

- 比如23,它默认就为int类型;如果加上后缀L,则为long类型;
- 比如23.8,它默认为double类型;如果加上后缀F,则为float类型;

Next define various types of variables and use these 8 basic data types.

public class TypeDemo1 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握8种基本数据类型,用来定义变量。
        // 1、整型
        byte number = 98;
        System.out.println(number);

        short number2 = 9000;

        int number3 = 12323232; // 默认

        // 注意:随便写一个整型字面量,默认是int类型的,73642422442424虽然没有超过long的范围,但是它超过了本身int的范围了。
        // 如果希望随便写一个整型字面量是long类型的,需要在其后面加上L/l
        long number4 = 73642422442424L;

        // 2、浮点型
        //注意:
        //随便写一个小数字面量,默认当成double类型对待的,
        //如果希望这个小数是float类型的,需要在后面加上:F/f
        float score1 = 99.5F;
        double score2 = 99.8; // 默认定义方案。

        // 3、字符型
        char ch1 = 'a';
        char ch2 = '中';
        char ch3 = '国';

        // 4、布尔型
        boolean b1 = true;
        boolean b2 = false;

        // 引用数据类型:String.
        // String代表的是字符串类型,定义的变量可以用来记住字符串。
        String name = "黑马";
        System.out.println(name);
    }
}

3. Data type conversion

3.1 Automatic type conversion

Dear students, let’s learn about type conversion next. Why should we learn type conversion? Because in our actual development, the value of a certain type of variable may be assigned to another type of variable; there may also be situations where data of multiple data types are operated together.

In the above situations, type conversion is actually involved. There are generally two types of type conversion, one is automatic type conversion , and the other is forced type conversion . Let’s learn about automatic type conversion first

  • What is automatic type conversion?
答:自动类型转换指的是,数据范围小的变量可以直接赋值给数据范围大的变量
	byte a = 12; 
	int b = a; //这里就发生了自动类型转换(把byte类型转换int类型)
  • How does automatic type conversion work?
答:自动类型转换其本质就是在较小数据类型数据前面,补了若干个字节

Insert image description here

In addition to conversion between byte and int, other types can also be converted. The conversion sequence is as shown in the figure below

Insert image description here

Below we demonstrate through code the various forms of automatic type conversion.

public class TypeConversionDemo1 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:理解自动类型转换机制。
        byte a = 12;
        int b = a; // 发生了自动类型转换了
        System.out.println(a);
        System.out.println(b);

        int c = 100; // 4
        double d = c;// 8 发生了自动类型转换了
        System.out.println(d);

        char ch = 'a'; // 'a' 97 => 00000000 01100001
        int i = ch; // 发生了自动类型转换了 =>  00000000 00000000  00000000 01100001
        System.out.println(i);
    }
}
  • Automatic type conversion of expressions

There is another form of automatic type conversion, which is automatic type conversion of expressions. The so-called expression refers to a formula in which several variables or data participate in operations together.

If variables or data of different types appear in the same expression and are operated together, what data type is the result of the operation in this case? The following two operation rules need to be followed:

1.多种数据类型参与运算,其结果以大的数据类型为准
2.byte,short,char 三种类型数据在和其他类型数据运算时,都会转换为int类型再运算

Next let’s look at the code demo and try it yourself

public class TypeConversionDemo2 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握表达式的自动类型转换机制。
        byte a = 10;
        int b = 20;
        long c = 30;
        long rs = a + b + c;
        System.out.println(rs);

        double rs2 = a + b + 1.0;
        System.out.println(rs2);
		
        byte i = 10;
        short j = 30;
        int rs3 = i + j;
        System.out.println(rs3);

        // 面试笔试题: 即使两个byte运算,结果也会提升为int
        byte b1 = 110;
        byte b2 = 80;
        int b3 = b1 + b2;
        System.out.println(b3);
    }
}

3.2 Forced type conversion

We learned about automatic type conversion earlier. We know that data with a small data type can be directly assigned to a variable with a large data range. On the other hand, can data with a large data range be directly assigned to a variable with a small data range? The answer is that an error will be reported.

Because data with a large data range is assigned to a variable with a small data range, it may not be able to hold it; just like pouring a large bucket of water into a small bucket, there is a risk of overflow.

  • What is cast

But you can also forcibly assign data with a large range to a variable with a small range. Forced type conversion is required here. The following is the format of forced type conversion

目标数据类型  变量名  =  (目标数据类型)被转换的数据;

The following is a code demonstration of forced type conversion

public class TypeConversionDemo3 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握强制类型转换。
        int a = 20;
        byte b = (byte) a;  // ALT + ENTER 强制类型转换。
        System.out.println(a);
        System.out.println(b);

        int i = 1500;
        byte j = (byte) i;
        System.out.println(j);

        double d = 99.5;
        int m = (int) d; // 强制类型转换
        System.out.println(m); // 丢掉小数部分,保留整数部分
    }
}
  • The principle of forced type conversion

    The principle of forced type conversion is actuallyforcibly cutting off the first few bytes, but there is a risk of data loss.

Insert image description here

We have finished learning about data types and data type conversion. Everyone can know when automatic type conversion will occur and how to perform forced type conversion.

4. Operators

Next, I will explain to the students a piece of content that is used a lot in development, called operators.

As we all know, computers are used to process data. To process data, calculation of data is indispensable. If you want to calculate data, you must use operators.

Operators are symbols that participate in operations. There are many kinds of operators provided by Java, which can be divided into the following types of arithmetic:

  • basic arithmetic operators
  • increment and decrement operators
  • assignment operator
  • Relational operators
  • Logical Operators
  • ternary operator

3.1 Arithmetic operators

Let’s start with the most basic arithmetic operators. The arithmetic operators are + - * / % , among which * means multiplication, / means division, % means taking the remainder

We need to pay attention to the following points

/: 两个整数相除,结果也是一个整数
%: 表示两个数相除,取余数

Insert image description here

What we need to pay attention to is: In addition to being used for addition operations, the + symbol can also be used as a connector. The + symbol is used as a connector when operating on a string, and the result is still a string .

The following code demonstrates the operation effects of various arithmetic operators.

public class OperatorDemo1 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握基本的算术运算符的使用。
        int a = 10;
        int b = 2;
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b); // 20
        System.out.println(a / b); // 5
        System.out.println(5 / 2); // 2.5 ==> 2
        System.out.println(5.0 / 2); // 2.5
        int i = 5;
        int j = 2;
        System.out.println(1.0 * i / j); // 2.5

        System.out.println(a % b); // 0
        System.out.println(3 % 2); // 1

        System.out.println("---------------------------------------");

        // 目标2:掌握使用+符号做连接符的情况。
        int a2 = 5;
        System.out.println("abc" + a2); // "abc5"
        System.out.println(a2 + 5); //  10
        System.out.println("itheima" + a2 + 'a'); // "itheima5a"
        System.out.println(a2 + 'a' + "itheima"); // 102itheima
    }
}

3. 2 Increment and decrement operators

Next, learn one of the more commonly used operators: ++ and --

++ is read as incrementing, -- is read as decrementing; the operation rules are as follows

Insert image description here

What we need to pay attention to is that self-increment and self-decrement can only operate on variables, not literals. Specific use can also be divided into two situations, as follows:

1.单独使用:++或者--放在变量前面没有区别
	   int a =10; 
	    a++;  //11
		--a;  //10
		System.out.println(a); //10

2.混合使用:++或者--放在变量或者前面运算规则稍有不通过
	//++在后:先做其他事情,再做自增和自减
	int a = 10;
	int b = a++; //等价于 int b = a; a++; 

	//++在前:先自增或者自减,再做其他运输
	int x = 10;
	int y = --x; //等价于x--; int y = x;  

The following demonstrates the usage of ++ and -- through code

public class OperatorDemo2 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握自增自减运算符的使用。
        int a = 10;
        // a++; // a = a + 1
        ++a;
        System.out.println(a);

        // a--; // a = a - 1
        --a;
        System.out.println(a);

        // 自增自减只能操作变量不能操作字面量的,会报错!
      	//System.out.println(2++);

        System.out.println("--------------------------------------");

        int i = 10;
        int rs = ++i; // 先加后用
        System.out.println(rs);
        System.out.println(i);

        int j = 10;
        int rs2 = j++; // 先用后加
        System.out.println(rs2);
        System.out.println(j);
    }
}

3.3 Assignment operator

Next, we learn the assignment operator. The basic assignment operator is actually =, which means assigning the data on the right to the variable on the left.

int a = 10; //将数据10赋值给左边的变量a

In addition to the basic assignment operators, we will mainly learn about the extended assignment operators here. have+= -= *= /= %=

Insert image description here

Let’s take+= as an example to look at its operation rules. Other operators can be analyzed in the same way

int a = 10;
//+=解析:在a原来记录值10的基础上累加5,将结果重新赋值给a; 
a+=5; 
//最终打印a的值为15
System.out.println(a); 

Let’s demonstrate it to you through an example of the first red envelope.

public class OperatorDemo3 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握扩展赋值运算符的使用。
        // +=
        // 需求:收红包
        double a = 9.5;
        double b = 520;
        a += b;  // a = (double)(a + b);
        System.out.println(a);

        // -= 需求:发红包
        double i = 600;
        double j = 520;
        i -= j;  // i = (double)(i - j);
        System.out.println(i);

        int m = 10;
        int n = 5;
        // m *= n; // 等价形式: m = (int)(m * n)
        // m /= n; // 等价形式: m = (int)(m / n)
        m %= n;    // 等价形式: m = (int)(m % n)
        System.out.println(m);
    }
}

After learning the basic use of the extended assignment operator, let’s look at an interview question

问题1:下面的代码否有问题?
    byte x = 10;
    byte y = 30;
	x = x + y;  //这句代码有问题,因为两个byte类型数据相加,会提升为int类型;
	
问题2:下面的代码是否有问题?
	byte x = 10;
	byte y = 30;
	x+=3; //这句代码没有问题,因为这里有隐含的强制类型转换
		  //x+=3; 等价于 byte x = (byte)(x+y);

At this point, we have finished studying the assignment operator. Let’s briefly summarize it.

1.基本赋值运算符:![在这里插入图片描述](https://img-blog.csdnimg.cn/7efc140675a74ff08c4c4717b1aff224.png#pic_center)

	=符号含义: 把右边的值赋值给左边的变量
	
2.扩展赋值运算符:
	+= -= *= /= %=符号含义:将右边的数据和左边的变量相加、相减、相乘、相除、取余数后,将结果重新赋值给左边的变量。

3.4 Relational operators

Next, we will learn an operator that is used a lot in actual code, but is also very simple, called a relational operator. Relational operators (also called comparison operators).

The following figure shows the symbols and functions of each relational operator. The result of each relational operator is false.

Let’s demonstrate the effects of various relational operators through code.

public class OperatorDemo4 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握关系运算符的基本使用。
        int a = 10;
        int b = 5;
        boolean rs = a > b;
        System.out.println(rs);

        System.out.println(a >= b); // 要么a大于b,或者a等于b
        System.out.println(2 >= 2); // true
        System.out.println(a < b);
        System.out.println(a <= b); // false
        System.out.println(2 <= 2); // true
        System.out.println(a == b); // false
        System.out.println(5 == 5); // true
        
        // 注意了:判断是否相等一定是用 == ,=是用来赋值的。
        // System.out.println(a = b); 
        System.out.println(a != b); // true
        System.out.println(10 != 10); // false

        System.out.println(false ^ true ^ false);
    }
}

Now we only need to know the operation effect of each relational operator. Regarding the actual application of relational operators, you need to learn the flow control statement later to actually use it. .

Relational operators are often used in conditional judgments in programs. Depending on whether the result of the conditional judgment is true or false, the subsequent operations to be performed are determined.

3.5 Logical operators

After learning about relational operators, let’s learn about logical operators. Let’s take a look at what logical operators are.

Logical operators are used to operate multiple conditions together, and the final result is true or false.

Insert image description here

Below we use several cases to demonstrate the use of logical operators

//需求1:要求手机必须满足尺寸大于等于6.95,且内存必须大于等于8.
//需求2:要求手机要么满足尺寸大于等于6.95,要么内存必须大于等于8.
public class OperatorDemo5 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握逻辑运算符的使用。
        // 需求:要求手机必须满足尺寸大于等于6.95,且内存必须大于等于8.
        double size = 6.8;
        int storage = 16;
        // 1、& 前后的条件的结果必须都是true ,结果才是true.
        boolean rs = size >= 6.95 & storage >= 8;
        System.out.println(rs);

        // 需求2:要求手机要么满足尺寸大于等于6.95,要么内存必须大于等于8.
        // 2、| 只要多个条件中有一个是true,结果就是true.
        boolean rs2 = size >= 6.95 | storage >= 8;
        System.out.println(rs2);

        // 3、!取反的意思
        System.out.println(!true); // false
        System.out.println(!false); // true
        System.out.println(!(2 > 1)); // false

        // 4、^ 前后条件的结果相同时返回false,不同时返回true.
        System.out.println(true ^ true); // false
        System.out.println(false ^ false); // false
        System.out.println(true ^ false); // true
        System.out.println(false ^ true); // true

        // 5、&& 左边为false,右边不执行。
        int i = 10;
        int j = 20;
        // System.out.println(i > 100 && ++j > 99);
        System.out.println(i > 100 & ++j > 99);
        System.out.println(j);

        // 6、|| 左边是true ,右边就不执行。
        int m = 10;
        int n = 30;
        // System.out.println(m > 3 || ++n > 40);
        System.out.println(m > 3 | ++n > 40);
        System.out.println(n);
    }
}

Now that I have learned the rules about logical operators, I just need to give you an operation expression and you can analyze the result. As for the actual application of logical operators, you need to learn the flow control statements before you can actually use them.

Logical operators are often used in programs to combine several conditional judgments. Depending on whether the result of the conditional judgment is true or false, the subsequent operations to be performed are determined.

3.6 Ternary operator

Next, we learn the last operator today, called the ternary operator.

First, let’s get to know the format of the ternary operator:

关系表达式?1 :2;

The execution process of ternary operation: first calculate the value of the relational expression. If the value of the relational expression is true, the value 1 is returned; if the value of the relational expression is false, the value 2 is returned;

As shown in the figure below: Determine whether the student's score is >= 60. If it is true, the exam is passed; if it is false, the score is unqualified.

Insert image description here

Next, I will demonstrate it through code, so that everyone can master the format and execution process of the ternary operator.

public class OperatorDemo6 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握三元运算符的基本使用。
        double score = 58.5;
        String rs = score >= 60 ? "成绩及格" : "成绩不及格";
        System.out.println(rs);

        // 需求2:找出2个整数中的较大值,并输出。
        int a = 99;
        int b = 69;
        int max = a > b ? a : b;
        System.out.println(max);

        // 需求3:找3个整数中的较大值。
        int i = 10;
        int j = 45;
        int k = 34;

        // 找出2个整数中的较大值。
        int temp = i > j ? i : j;
        // 找出temp与k中的较大值。
        int max2 = temp > k ? temp : k;
        System.out.println(max2);
    }
}

3.7 Operation priority

Finally, we will introduce to you the priority of operators. If you want to know the priority of each operator, which ones are calculated first and which ones are calculated last, you can refer to the picture below.

Insert image description here

From the figure, we find that the && operation has a higher priority than the || operation, so when && and || exist at the same time, && is calculated first and then ||;

For example, the following code

//这里&&先算 相当于 true || false 结果为true
System.out.println(10 > 3 || 10 > 3 && 10 < 3); // true

Finally, let me tell you that in actual development, we rarely consider the priority of operations, because if you want some data to be calculated first, just add () , which makes it more readable.

//有括号先算 相当于 true && false 结果为false
System.out.println((10 > 3 || 10 > 3) && 10 < 3); //false

5. Case technology: Obtaining data entered by the user’s keyboard

Finally, I will tell you about a case technology. This technology is actually a bit advanced now because it requires the knowledge learned later. But talking about it here can make our learning experience better. The data involved in the calculation in the previous case were hard-coded in the program. Next, we want to enter the data with the keyboard and then participate in the running of the program.

Keyboard entry is actually not something we do ourselves, but Java itself provides such a function, and we can just call it according to its requirements.

When we install the JDK, the JDK actually already contains some code written in Java. We can just use the code written in Java directly.

比如:Scanner就是Java提供给我们用于键盘录入数据的类,为了录入不同类型的数据,还提供了不同的功能,每一个功能会有不同的名称,我们只需要调用Scanner提供的功能就可以完成键盘录入数据。

You only need to follow the steps below to write the code and you can enter data with the keyboard.

【第1步】:在class类上导包:一般不需要我们自己做,idea工具会自动帮助我们 导包的。
	import java.util.Scanner;
	
【第2步】:得到一个用于键盘扫描器对象(照抄代码就行,固定格式)
	//Scanner是键盘扫描器对象(你就把它理解成一个东西),这个东西有录入的功能
	//sc是给这个东西取的名字
	Scanner sc = new Scanner(System.in);

【第3步】:开始调用sc的功能,来接收用户键盘输入的数据。
	//sc这个东西有键盘录入整数的功能,这个功能的名字叫nextInt()
	//.表示表示调用的意思
	int age = sc.nextInt();
	System.out.println("我的年龄是:"+age);

	//sc这个东西还有键盘录入字符串的功能,这个功能的名字叫next
	String name = sc.next();
	System.out.println("我的姓名是:"+name);

The following is a complete code demonstration

public class ScannerDemo1 {
    
    
    public static void main(String[] args) {
    
    
        // 1、导包:一般不需要我们自己做,idea工具会自动帮助我们 导包的。
        // 2、抄写代码:得到一个键盘扫描器对象(东西)
        Scanner sc = new Scanner(System.in);

        // 3、开始 调用sc的功能,来接收用户键盘输入的数据。
        System.out.println("请您输入您的年龄:");
        int age = sc.nextInt(); // 执行到这儿,会开始等待用户输入一个整数,直到用户按了回车键,才会拿到数据。
        System.out.println("您的年龄是:"  + age);

        System.out.println("请您输入您的名字:");
        String name = sc.next(); // 执行到这儿,会开始等待用户输入一个字符串,直到用户按了回车键,才会拿到数据。
        System.out.println(name + "欢迎您进入系统~~");
    }
}

= new Scanner(System.in);

[Step 3]: Start calling the function of sc to receive data entered by the user's keyboard.
//sc has the function of inputting integers from the keyboard. The name of this function is nextInt()
//. It means calling
int age = sc.nextInt();
System.out.println(“My age is:”+age);

//sc这个东西还有键盘录入字符串的功能,这个功能的名字叫next
String name = sc.next();
System.out.println("我的姓名是:"+name);

下面是完整代码演示

```java
public class ScannerDemo1 {
    public static void main(String[] args) {
        // 1、导包:一般不需要我们自己做,idea工具会自动帮助我们 导包的。
        // 2、抄写代码:得到一个键盘扫描器对象(东西)
        Scanner sc = new Scanner(System.in);

        // 3、开始 调用sc的功能,来接收用户键盘输入的数据。
        System.out.println("请您输入您的年龄:");
        int age = sc.nextInt(); // 执行到这儿,会开始等待用户输入一个整数,直到用户按了回车键,才会拿到数据。
        System.out.println("您的年龄是:"  + age);

        System.out.println("请您输入您的名字:");
        String name = sc.next(); // 执行到这儿,会开始等待用户输入一个字符串,直到用户按了回车键,才会拿到数据。
        System.out.println(name + "欢迎您进入系统~~");
    }
}

Guess you like

Origin blog.csdn.net/Coastlise/article/details/130295348