# Supplement (data input scanner package)

How to use scanner?

1. Guide Package

import java.util.Scanner;

2. Create object

Scanner sc = new Scanner(System.in);

sc: it belongs to variable name, variable, and others are immutable

3. Receive data

int i = sc.nextInt();

i: It belongs to the variable name, which is variable, and others are immutable. nextInt() is one of the methods in the scanner package to obtain the data integer type.

# operator

## 1. What is an operator?

Operator: an operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. Symbols for operating on constants or variables (+ - * /%)

Expression: an expression that connects constants or variables with operators and conforms to java syntax can be called an expression. Expressions connected by different operators represent different types of expressions. (a+b)

## 2. Operator classification

### (1) Arithmetic operators (+,, *, /,% (remainder))

/The difference between (multiplication) and% (remainder): divide the two data, / take the quotient of the result, and% take the remainder of the result.

matters needing attention:

Integer operation can only get integers. To get decimals, floating-point numbers must be involved in the operation.

Take the value corresponding to the character at the bottom of the computer for calculation ('a'=97,'A'=65 (A~Z,a~z),' 0 '= 48 (0 ~ 9))

When an arithmetic expression contains values of multiple basic data types, the type of the entire arithmetic expression will be promoted automatically.

Emphasis: the character "+" operation is an addition operation String "+" operation is to splice two strings

In the "+" operation, if a string appears, it is a connection operator, otherwise it is an arithmetic operation. When the "+" operation is performed continuously, it is performed one by one from left to right. For example: 1 + 99 + "year dark horse" results output 100 year dark horse.

### (2) Assignment operator

matters needing attention:

The extended assignment operator implies a cast

### (3) Self increasing and self decreasing operator

Note: x + + means to use first and then add, + + X means to add first and then use "--" is the same

### (4) Relational operator

### (5) Logical operator

Short circuit logic operator:

Common operators: & &, |, |,!

Note: logic and &: whether the left is true or false, the right statement must be executed.

Short circuit & &: if the left is true, the right statement will be executed; otherwise, if the left is false, the right statement will not be executed.

Logical or |: whether the left is true or false, the right statement must be executed.

Short circuit or |: if the left is false, the statement on the right is executed; If true, the statement on the right will not be executed.

### (6) Priority operator

### (7) Ternary operator

Format: relational expression? Expression 1: expression 2 (for example: a > b? A: b)

How to judge the return value?

First, the value of the relationship expression is calculated. If the value is true, the value of expression 1 is returned; otherwise, it is false, and the value of expression 2 is returned.

# Process control statement

## 1. Sequential structure

Relatively simple: execute the code in sequence

## 2. Branch structure (if,switch)

### (1) if statement:

Example demonstration:

/* My dog is 5 years old. How old is a 5-year-old dog equivalent to a human? In fact, each year of the first two years of a dog is equivalent to 10.5 years of human age, and then every additional year will increase by four years. So how old is a 5-year-old dog equivalent to a human? It should be: 10.5 + 10.5 + 4 + 4 + 4 = 33 years old. Write a program to obtain the age of the dog entered by the user, and display it through the program, which is equivalent to the age of human beings. If the user enters a negative number, a prompt is displayed. */ import java.util.Scanner; public class Demo5 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter the dog's age:"); double year = sc.nextDouble(); double peopleYear=0; if (year>=2){ peopleYear=10.5*2+(year-2)*4; }else if (year>0&&year<2){ peopleYear=year*10.5; }else { System.out.println("You entered the dog's age incorrectly"); } System.out.println("Dog age"+year+"Equivalent to human age"+peopleYear); } }

### (2) switch statement:

Example demonstration

import java.util.Scanner; /* Analog calculator function, add, subtract, multiply and divide the two int type data entered by the keyboard, and print the operation results. requirement: Enter three integers on the keyboard. The first two integers represent the data participating in the operation, and the third integer is the operation to be performed (1: addition operation, 2: subtraction operation, 3: multiplication operation, 4: division operation). The demonstration effect is as follows: Please enter the first integer: 30 Please enter the second integer: 40 Please enter the operation you want to perform (1: add, 2: subtract, 3: multiply, 4: divide): 1 Console output: 30 + 40 = 70 */ public class DemoSwitch { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter the first integer"); int a=sc.nextInt(); System.out.println("Please enter the second integer" ); int b = sc.nextInt(); System.out.println("Operation type required: 1:Represents an addition operation,2:Represents subtraction,3:Represents multiplication,4:Represents the division operation"); int e=sc.nextInt(); switch (e){ case 1:System.out.println(a+"+"+b+"="+(a+b));break; case 2:System.out.println(a+"-"+b+"="+(a-b));break; case 3:System.out.println(a+"*"+b+"="+(a*b));break; case 4:System.out.println(a+"/"+b+"="+(a/b));break; default:System.out.println("The operation type entered is incorrect!!");break; } } }

Note: expression: the values are byte, short, int, char. After jdk5, it can be enumeration, and after JDK7, it can be String break: indicates that it is used to end the switch statement (jump out of the loop)

countinue: means to jump out of the loop, not execute the following statements, and carry out the next loop

Summary: if (condition) {...} , If (condition) {...} else {...} , If (condition) {...} else if (condition) {...}... Else {...} If the condition is true, execute the statement in {...}

## 3. Loop structure (for loop, do...while loop, while loop)

### (1) for loop

Example demonstration:

/* It is known that 2019 is the year of the pig. Please output all the years from 1949 to 2019 on the console */ public class Demo1 { public static void main(String[] args) { for (int i = 1949; i < 2019; i++) { //2. If the difference between the year and 2019 is a multiple of 12, it indicates that it is the year of the pig if ((2019 - i) % 12 == 0) { //3. Print the qualified year System.out.println(i); } } } }

### (2) do...while loop

Example demonstration

// Enter the score to judge whether you pass or not, and exit if you fail Scanner sc = new Scanner(System.in); do{ System.out.println("Please enter your grade:"); int score = sc.nextInt(); }while(score>60) System.out.println("I'm sorry you failed!");

### (3) while loop

Example demonstration

/* It is known that 2019 is the year of the pig. Please output all the years from 1949 to 2019 on the console */ public class Demo1 { public static void main(String[] args) { // int year = 1949; while (1949<=year&&year<=2019){ if (year%12==0){ System.out.println(year+"Year of the pig"); } year++; } } }

### (4) The difference between the three cycles

### (5) Loop nesting

What is nesting?

loop nesting

catalogue

Supplement (data input scanner package)

(1) Arithmetic operators (+,, *, /,% (remainder))

(3) Self increasing and self decreasing operator

2. Branch structure (if,switch)

3. Loop structure (for loop, do...while loop, while loop)

(4) The difference between the three cycles

: a loop contains another complete loop structure, which is called loop nesting. Nested loops can also be nested in embedded loops, which is called multi-layer loops.