1. Basic condition Structure
Place relational or logical operations in if () {//parentheses
}else{
}
2. Nested conditional structure//with two or more sets of conditions for combination judgment
If (condition 1) {
Condition 1 Establishment is the premise
If (condition 2) {
Condition 1 and Condition 2 are established
}else{
Condition 1 established, Condition 2 not established
}
}else{
Condition 1 is not tenable, is the premise
If (condition 3) {
Condition 1 not established, condition 3 established
}else{
Condition 1 is not established, condition 3 is not established
}
3. Multiple conditional structures//when there is a need to judge a continuous data segment, select multiple bars with ordered rules and the nearest principle
If (condition 1) {
Condition 1 established
}else if (condition 2) {
satisfies the condition 2 without satisfying the condition 1
}else if (condition 3) {
satisfies the condition 3 without satisfying the condition 2
}else if (condition 4) {
satisfies the condition 4 without satisfying the condition 3
}else{
When the condition is not met 4
}
4, select structure switch//logic simple efficiency excellent scope of application narrow
Switch (???) {//Data being compared (JDK 1.8 before int byte char SHORTB 1.8 string can be compared)
Case Comparison Value:
Compare the operation after successful;
Break Jump out of a switch statement block
Case Comparison Value:
Compare the operation after successful;
Break Jump out of a switch statement block
Case Comparison Value:
Compare the operation after successful;
Break Jump out of a switch statement block
....
Default
Action Statement
Break
}
Note: The switch has a run-through function when the case statement in the SWICTH structure cancels the break jump statement.
eg
Displays the number of days of the month, based on the month entered by the user, taking into account the February flat leap year scenario
Borrowing tools
Import Java.util.Scanner;
public class test{
public static void Main (string[] args) {
Request a tool for use
Scanner input=new Scanner (system.in);
Prompt user
System.out.println ("Please enter the current month:");
Open up memory space to receive user input data
int Month=input.nextint ();
Make a switch judgment
Switch (month) {
CASE1:
CASE3:
CASE5:
CASE7:
Case8:
Case10:
Case12:
System.out.println ("The number of days in your current month is: 31 days");
Break
CASE2:
System.out.println ("Please enter the current year is:");
int Year=input.nextint ();
if (year%4==0&&year%100!=0| | year%400==0) {
System.out.println ("The number of days in your current month is: 29 days");
}else{
System.out.println ("The number of days in your current month is: 28 days");
}
Break
Default
System.out.println ("The number of days in your current month is: 30 days");
Break
}
}
}
Java Conditional structure