# Day4: enter, if, switch

Posted by dark_destroyer on Thu, 03 Mar 2022 11:44:02 +0100

# Day4: enter, if, switch

1. When implementing branch judgment, a good habit is to put the judgment with high matching success rate in front, which can improve efficiency
2. In the expression judgment, we try to use the determined value to judge with the uncertain value

Execution process in the program:

1. Sequencing: starting from the main method, execute line by line

2. Selectivity: branch statement with selectivity [learn now]

3. Circularity: loop statements, repeatedly execute some code [learn tomorrow]

## 1. Console entry

Console entry: used to receive the value entered from the console

Steps:

1. Instantiate Scanner object

2. Call the method to receive the entered value; Methods: nextInt,next (commonly used)

```public class Console {
public static void main(String[] args) {
```

Enter an integer from the console and print out the multiple of the number

```Scanner sc = new Scanner();
System.out.print("Please enter an integer:"); //Print: print without line breaks
int num = sc.nextInt();  //The blocking method receives an integer value of 3
//Inputmismatch exception: enter an integer. If you enter another integer, an exception will be reported
System.out.println(num*2);  //6
sc.close();
```

Receive the value of the string

```String s = sc.next(); //Receive the value of a string
System.out.println("The value is:"+s);
s.close();
```

Receive a value of type double

```double d = sc.nextDouble(); //Receive a value of type double
System.out.println("The decimal value is:"+d);
d.close();
```

Gets the first character of the string, starting with a subscript of 0

```char c = sc.next().charAt(0); //Gets the first character of the string, starting with a subscript of 0
System.out.println(c);
c.close();
```

## 2.iF branch statement

• Single branch structure:
```if(Relational expression){
Sentence body;
}
```
```	Execution process: if the relationship expression is true, execute the statement body
```

Case: if you pass the PP exam, invite the whole class to dinner

```Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if(score>=60) {
System.out.println("Invite the whole class to dinner");
}
```
• If double branch structure: if else
```if(Relational expression){
Statement body 1:
}else{
Statement body 2:
}
```
```	Execution process: if If the relational expression holds, execute statement body 1; otherwise, execute statement body 2
```

Case: judge whether a year is a leap year

```		System.out.println("Enter a year");
Scanner input = new Scanner(System.in);
int y = input.nextInt();
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
System.out.println("It's a leap year");
} else {
System.out.println("It's a normal year");
}
input.close();
```
• if multi branch structure:
```if(Relationship expression 1){
Statement body 1;
}else if(Relational expression 2){
Sentence body 2;
}...
else if(Relational expression n){
Statement body n；
}else{//not essential
Sentence body;
}
```

Enter a score and divide the interval according to the size of the score

• No nesting

Case 1: score your grades

```		//No nesting
System.out.println("Please enter your grade");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score >= 90 && score <= 100) {
System.out.println("A");
} else if (score >= 60 && score < 90) {
System.out.println("B");
} else if (score >= 0 && score < 60) {
System.out.println("C");
} else {
System.out.println("The score you entered is illegal!");
}
sc.close();
```
```  Note: the judgment rules of multiple branches are either from small to large or from large to small
```
• If nested 1: if is included in the if judgment

Case 2: score your grades

```		System.out.println("Please enter your grade");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score>=0&&score<=100) {
if (score==100) {
System.out.println("Congratulations, full marks");
}else if (score>=90) {
System.out.println("excellent");
//Compared with the above multi branch structure, the branch judgment is optimized here
}else if (score>=60) {
System.out.println("pass");
}else if (score>=0) {
System.out.println("fail,");
}
}else{
System.out.println("The score you entered is illegal");
}
sc.close();
```
• If nesting 2: besides if nesting judgment, there is if nesting judgment

Case 3: make different follow-up choices according to the weather

```		Scanner input = new Scanner(System.in);
System.out.println("What's the weather like, 1 sunny day, 2 haze");
int weather = input.nextInt();

if (weather == 1) {
System.out.println("Today is a fine day. 1 go to the park and 2 go shopping");
int flag = input.nextInt();
if (flag == 1) {
System.out.println("Go to the park with your girlfriend");
} else if (flag == 2) {
System.out.println("Go shopping with your girlfriend");
} else {
System.out.println("error input");
}
} else if (weather == 2) {
System.out.println("The weather is bad today. 1. I watch movies at home and 2. I play games at home");
int flag = input.nextInt();
if (flag == 1) {
System.out.println("Watch a movie at home with your girlfriend");
} else if (flag == 2) {
System.out.println("Play games at home with your girlfriend");
} else {
System.out.println("error input");
}
} else {
System.out.println("error input");
}
input.close();
```

## 3.switch branch statement

• Relevant rules of switch statement

1. The return value of the expression must be one of the following types: int, byte, char, short, jdk1 String is supported after 7;
2. The value in the case clause must be constant, and the value in all case clauses should be different;
3. The default clause is optional;
4. The break statement is used to make the program call out the switch statement block after executing a case branch. If there is no break written after the case, it will be executed directly below!
5. The executor behind the case can write {} or not {}
```switch(variable){
case Value 1:Statement body 1;
break;  //Jump out of s witch statement
case Value 2:Statement body 2;
break;
...
case value n:Statement body n;
break;
default:Sentence body; n+1;
break;
}
```

Case 1: Please input Monday to Friday and print what to eat respectively

```		System.out.println("Please enter Monday through Friday (1)～5)");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("food1");
break;
case 2:
System.out.println("food2");
break;
case 3:
System.out.println("food3");
break;
case 4:
System.out.println("food4");
break;
case 5:
System.out.println("food5");
break;
default:
System.out.println("error input");
break;
}
```
• switch and if are nested, and variables are defined in case

Case 2: enter 1 ~ 7, representing 7 days of a week respectively

```		System.out.println("Please enter 1~7");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("Today is Monday. It's quite crowded. 1 take the bus and 2 take the subway");
int method = input.nextInt();
if (method == 1) {
System.out.println("by subway");
} else if (method == 2) {
System.out.println("ride in a subway train");
} else {
System.out.println("Other travel modes");
}
break;
case 2:
int a = 20;
//After defined here, it can be used in the whole braces and can no longer be defined
//Even if the statement is not executed, it is still defined
System.out.println("Today is Tuesday");
System.out.println(a);
break;
case 3:
a = 10;
System.out.println("Today is Wednesday");
System.out.println(a);
break;
case 4:
System.out.println("Today is Thursday");
break;
case 5:
System.out.println("Today is Friday");
break;
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("error input");
break;
}
input.close();
```

Case 3: Please input the names of the four heavenly kings and print out their specialties

• Details: after jdk7, the variable of switch can be a String class
```		System.out.println("Please enter the names of the four heavenly kings");
Scanner input = new Scanner(System.in);
String name = input.next();
switch (name) {
case "Lau Andy":
System.out.println("Versatile artist");
break;
case "Xue You Zhang":
System.out.println("Song god");
case "dawn":
System.out.println("Handsome");
case "Guo Fucheng":
System.out.println("Dance king");
default:System.out.println("error input");
break;
input.close();
```

Compared with switch, if tends to be used in judgment range and switch tends to be used in equivalence judgment

## 4. Local variables

Variables appearing in the main method body or parameters are local variables

characteristic:

1. You must assign a value before you can use it
2. Scope to definition to end of method body
3. You cannot define a variable with the same name in the same method
```public class Test1 {
public static void main(String[] args) {//args 	 Local variables of method parameters
int a = 3;//Local variables defined in the body of the main method
System.out.println(a);
//int a;Duplicate local variable a: you cannot define a variable with a duplicate name in the same method
}
//System.out.println(a); Cannot output here
}
```

## task

1. Zhang San's Java score is more than 80 points, and the whole class applauded
```package com.qf.Day3HomeWork;

import java.util.Scanner;

public class d3_1 {
public static void main(String[] args) {
System.out.println("Please enter Zhang San's score");
Scanner input = new Scanner(System.in);
int score = input.nextInt();
if (score>=0&&score<=100) {
if(score>80) {
System.out.println("The whole class applauded");
}

}else {
System.out.println("error input");
}
input.close();
}
}
```
1. Input Zhang San's Java and php scores, which are greater than 80 points. The whole class applauds, otherwise it is encouraged
```package com.qf.Day3HomeWork;

import java.util.Scanner;

public class d3_2 {
public static void main(String[] args) {
System.out.println("Please enter the name of Zhang San Java achievement");
Scanner input = new Scanner(System.in);
int JavaScore = input.nextInt();
System.out.println("Please enter the name of Zhang San php achievement");
int PhpScore = input.nextInt();
if (JavaScore>=0&&JavaScore<=100&PhpScore>=0&&PhpScore<=100) {
if (JavaScore>=80&&PhpScore>=80) {
System.out.println("The whole class applauded");
}else {
System.out.println("encourage");
}
}else {
System.out.println("error input");
}
input.close();
}
}
```
1. Evaluation of examination results, more than or equal to 80 points, good, more than or equal to 60 points and less than 80 points, medium, less than 60 points, more efforts
```package com.qf.Day3HomeWork;

import java.util.Scanner;

public class d3_3 {
public static void main(String[] args) {
System.out.println("Please enter your grade");
Scanner input = new Scanner(System.in);
int score = input.nextInt();
if (score>=0&&score<=100) {
if(score>=80) {
System.out.println("good");
}else if(score>=60){
System.out.println("in");
}else {
System.out.println("Make more efforts");
}
}else {
System.out.println("error input");
}
}
}
```
1. Enter member points and shopping amount, enjoy different discounts according to different points, and output settlement amount
10% off for x < 2000
20% discount for 2000 ≤ x ＜ 4000
40% discount for 4000 ≤ x ＜ 8000
x ≥ 8000 60% discount
Tip: integral: 3000 500 * 0.8 = 400
Multi branch judgment based on integral
```package com.qf.Day3HomeWork;

import java.util.Scanner;

public class d3_4 {
public static void main(String[] args) {
System.out.println("Enter the user's membership points");
Scanner input1 = new Scanner(System.in);
int credits = input1.nextInt();
if (credits<0) {
System.out.println("error input");
}else {
System.out.println("Please enter user purchase amount");
Scanner input2 = new Scanner(System.in);
Double sum = input2.nextDouble();
if (credits<2000) {
System.out.println(sum*0.9);
}else if (credits>=2000&&credits<4000) {
System.out.println(sum*0.8);
}else if (credits>=4000&&credits<8000) {
System.out.println(sum*0.7);
}else if(credits>=8000){
System.out.println(sum*0.6);
}
}

}

}
```
1. Input the number and output the day of the week
```package com.qf.Day3HomeWork;

import java.util.Scanner;

public class d3_5 {
public static void main(String[] args) {
System.out.println("Enter number");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Thuesday");
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("error input");
break;
}
input.close();
}
}
```

Topics: Java JavaEE macOS