Flow control statement ten is a statement used to control the execution sequence of each statement in the program, and the statement can be combined into a small logic module that can complete a certain function.
Three basic process structures: sequence, branch, loop structure
1. Branch structure: (if-else: conditional judgment structure)
(1) Three structures
The first:
if(condition_expression) {
execute expression
}
The second type: choose one of the two
if(condition_expression) {
execute expression1
}else {
execute expression 2
}
The third option: choose one
if(condition_expression){
execute expression1
}else {
execute expression 2
}else {
execute expression 3
}
...
else {
execute expression n
}
illustrate:
1. The else structure is optional;
2. For and conditional expressions:
> If there is a "mutually exclusive" relationship (or no intersection relationship) between multiple conditional expressions, it doesn't matter which judgment and execution statement is declared above or below.
> If there is an intersection relationship between multiple conditional expressions, it is necessary to consider clearly which structure should be declared on it according to the actual situation.
> If there is an inclusion relationship between multiple conditional expressions, usually, the one with the smaller range needs to be declared above the one with the larger range. Otherwise, those with a small scope will have no chance to execute.
public class IfTest{ public static void main(String[] args){ //Example 1 int heartBeats = 79; if(heartBeats < 60 || heartBeats > 100){ System.out.println("need to do further inspection"); } System.out.println("end of inspection"); //Example 2 int age = 23; if(age < 18){ System.out.println("you can also watch cartoons"); }else { System.out.println("You can watch Chengren movie now"); } //Example 3 if(age < 0){ System.out.println("The data you entered is illegal"); }else if(age < 18){ System.out.println("adolescence"); }else if (age < 35){ System.out.println("young adulthood"); }else if(age < 60){ System.out.println("middle age"); }else if(age < 120){ System.out.println("old age"); }else { System.out.println("you are going to be immortal~~"); } } }
(2) Keyboard input: (How to use Scanner)
Requirement: How to get different types of variables from the keyboard: Need to use the Scanner class
Specific implementation steps:
1. Guide package: import java.util.Scanner;
2. Instantiation of Scanner (create Scanner object)
3. Call the relevant methods of the Scanner class (next() / nextXxx()) to obtain variables of the specified type
4. For the acquisition of char type, Scanner does not provide related methods, and can only obtain a string.
Note: You need to enter the value of the specified type according to the corresponding method. If the input data type does not match the required type, an exception will be reported: InputMisMatchException, causing the program to terminate.
import java.util.Scanner; public class Scanner{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int score = sc.nextInt(); System.out.println(score); //For the acquisition of char type, Scanner does not provide related methods, and can only obtain a string. System.out.println("Please enter your gender"); String gender = sc.next(); char genderChar = gender.charAt(0);//Get the character at index 0 System.out.println(genderChar); } }
(3) if-else example
Example 1:
import java.util.*; public class IfExer1{ public static void main(String[] args){ System.out.println("Please enter your score:"); Scanner sc = new Scanner(System.in); int score = sc.nextInt(); if(score < 0 || score > 100){ System.out.println("The data you entered is illegal"); }else if(score == 100){ System.out.println("reward one BWM"); }else if(score > 80 && score <100){ System.out.println("reward one iPhone x"); }else if(score >= 60 && score < 80){ System.out.println("reward one ipad"); }else { System.out.println("want to fart"); } } }
Example 2:
illustrate:
1. if-else results can be nested with each other.
2. If the execution statement in the if-else structure has only one line, the corresponding pair of {} can be omitted, but it is not recommended to omit.
import java.util.*; public class IfExer2{ public static void main(String[] args){ System.out.println("Please enter three integers:"); Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); int num3 = sc.nextInt(); if(num1 > num2){ if(num3 >= num1){ System.out.println(num2 + "," + num1 + "," + num3); }else if(num3 <= num2){ System.out.println(num3 + "," + num2 + "," + num1); }else{ System.out.println(num2 + "," + num3 + "," + num1); } }else{ if(num3 >= num2){ System.out.println(num1 + "," + num2 + "," + num3); }else if(num3 <= num1){ System.out.println(num3 + "," + num1 + "," + num2); }else{ System.out.println(num1 + "," + num3 + "," + num2); } } } }
Example 3:
public class IfExer3{ public static void main(String[] args){ int dogAge = 6; if(dogAge >= 0 && dogAge <= 2){ System.out.println("Equivalent to human age:" + dogAge * 10.5); }else if(dogAge > 2){ System.out.println("Equivalent to human age:" + (2 * 10.5 + (dogAge - 2) * 4)); }else{ System.out.println("The dog is not born yet!"); } } }
Example 4:
Obtaining random numbers: Math.random()//[0.0,1.0) formula:[a,b] : (int)(Math.random() * (b - a + 1) + a) source code: public class IfExer4{ public static void main(String[] args){ int value = (int)(Math.random() * 90 + 10); System.out.println(value); } }
Example 5:
import java.util.*; public class IfExer5{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Please enter your height:( cm)"); int height = sc.nextInt(); System.out.println("Please enter your wealth:(Ten million)"); double wealth = sc.nextDouble(); System.out.println("Please enter your handsomeness: (Yes/no)"); boolean isHandsome = sc.nextBoolean(); if(height >=180 && wealth >= 1 && isHandsome == true){ System.out.println("I want to marry him and have a monkey~~~"); }else if(height >=180 || wealth >= 1 || isHandsome == true){ System.out.println("Marry, it's okay~~~"); }else { System.out.println("Don't marry, dick~~"); } } } Typical code: System.out.println("Please enter your handsomeness: (Yes/no)"); String isHandsome = sc.next(); if(height >=180 && wealth >= 1 && isHandsome.equals("yes")){ System.out.println("I want to marry him and have a monkey~~~"); }
2. Branch structure (switch-case)
structure:
switch(expression) {
case constant 1:
Statement 1;
//break;
case constant 2:
Statement 2;
//break;
case constant 3:
Statement 3;
//break;
...
case constant n:
statement n;
//break;
default:
statement;
//break;
}
illustrate:
① According to the value in the switch expression, match the constants in each case in turn. Once the match is successful, enter the corresponding case structure and call its execution statement.
After calling the execution statement, it will continue to execute the execution statements in other case structures until it encounters the break keyword or the end of the secondary switch-case structure.
②break, can be used in the switch-case structure, indicating that once this keyword is executed, it will jump out. switch-case structure
③The expression in the switch structure can only be one of the following 6 data types: byte, short, char, int, enumeration type (JDK5.0), String type (JDK7.0)
④ After case, only constants can be declared, and scope cannot be declared.
⑤ The break keyword is optional in the switch-case structure. (Most need to add)
⑥default: equivalent to the else in the if-else structure; the default structure is optional; and the position is flexible (other positions need to add break).
public class SwitchCaseTest{ public static void main(String[] args){ int number = 2; switch(number){ case 0: System.out.println("Zero"); break; case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; case 3: System.out.println("Three"); break; case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); break; default: System.out.println("Other"); } } }
Example 1:
Note: If the execution statements of multiple cases in the switch-case structure are the same, you can consider merging.
public class SwitchCaseTest1{ public static void main(String[] args){ //More complicated way: (the output is the same, you can consider merging) int score = 78; switch(score / 10){ case 0: case 1: case 2: case 3: case 4: case 5: System.out.println("failed"); break; case 6: case 7: case 8: case 9: case 10: System.out.println("pass"); break; } //Optimization: switch(score / 60){ case 0: System.out.println("failed"); break; case 1: System.out.println("pass"); break; } //Simple way: int score = 78; if(score >= 60){ System.out.println("pass"); }else{ System.out.println("failed"); } } }
Example 2:
Note: break is optional in the switch-case structure
import java.util.*; public class SwitchCaseTest2{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Please enter the 2019 month: "); int month = sc.nextInt(); System.out.println("Please enter the 2019 day: "); int day = sc.nextInt(); //Define a variable to hold the total number of days //Method 1: (No more) /* int sumDays = 0; if(month == 1){ sumDays = day; }else if(month ==2){ sumDays = 31 + day; }else if(month == 3){ sumDays = 31 + 28 + day; }else if(month ==4){ sumDays = 31 + 28 + 31 + day; }else if(month ==){ }else if(month ==) }else if(month ==){ }else if(month ==) */ //Method 2: The switch-case structure is the same as if-else. //write backwards switch(month){ case 12: sumDays += 30; case 11: sumDays += 31; case 10: sumDays += 30; case 9: sumDays += 31; case 8: sumDays += 31; case 7: sumDays += 30; case 6: sumDays += 31; case 5: sumDays += 30; case 4: sumDays += 31; case 3: sumDays += 28; case 2: sumDays += 31; case 1: sumDays += day; } System.out.println("2019 year" + month + "moon" + day + "What day is the year" + sumDays + "sky"); } }
Example 3:
illustrate:
① Any structure that can use switch-case can be converted to if-else, and vice versa; not true.
② When we write a branch structure, when we find that we can use switch-case (at the same time, the value of the expression in switch is not too many), and we can use if-else, we prefer to use switch-case.
Reason: The execution efficiency of switch-case is slightly higher.
import java.util.*; public class SwitchCaseTest2{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("please enter year: "); int year = sc.nextInt(); System.out.println("please enter month: "); int month = sc.nextInt(); System.out.println("please enter day: "); int day = sc.nextInt(); //Define a variable to hold the total number of days int sumDays = 0; switch(month){ case 12: sumDays += 30; case 11: sumDays += 31; case 10: sumDays += 30; case 9: sumDays += 31; case 8: sumDays += 31; case 7: sumDays += 30; case 6: sumDays += 31; case 5: sumDays += 30; case 4: sumDays += 31; case 3: //sumDays += 28; Determine whether the year is a leap year if((year % 4 ==0 && year % 100 != 0)|| year % 400 == 0){ sumDays += 29; }else{ sumDays += 28; } case 2: sumDays += 31; case 1: sumDays += day; } System.out.println(year + "year" + month + "moon" + day + "day is the first day of the year" + sumDays + "sky"); } }