If… else…

(1) single branch structure (2) double branch structure (3) multi-branch structure

Statement structure: if(condition expression 1){block 1 to be executed when condition expression 1 is true; }else if(condition expression 2){block 2 to execute if condition expression 2 is true; }else if(condition expression 3){block 3 to be executed if condition expression 3 is true; }... Else {// Optional, write or not write block n+1 to execute when all conditional expressions are not set; }Copy the code

Execution features:

(1) Judge from top to bottom in order, if one condition is met, execute the corresponding statement block, the rest of the condition will not look at. If all conditions are not met, the statement block in the last else is executed.

(2) All conditional expressions in if() must be Boolean or have Boolean results

If {} and else {} have only one statement, you can omit {}

(4) In the multi-branch structure, whether the order of multiple IF conditions can be adjusted depends on the range of these conditions

A: It is mutually exclusive and can be adjusted

B: The inclusion relation cannot be adjusted. The small range is on the top and the large range is on the bottom

In a multi-branch structure, the order of multiple if conditions can be adjusted depends on the range of the conditions

(1) Mutually exclusive relationship, can be adjusted

(2) Inclusion relation can not be adjusted. The small range is above, and the large range is below

Select structure: switch… case

Syntax: switch(expression){case constant value 1: statement block 1; "Break," Case constant value 2: statement block 2; Case constant value 3: block 3; . Default :// This part is optional. The statement block n+1 can be written or not written. break; }Copy the code

Note: (1) Break is not mandatory

(2) Default is not mandatory, and the position is not necessarily at the end, but is traditionally at the end. No matter where it is, it is entered from default only if all cases are not satisfied.

(3) Case must be followed by constant value

(4) The constant after case cannot be repeated

(5) The result type of the expression in switch can only be the four basic data types of byte,short,int,char and two reference data types of String and enumeration.

Execution process:

(1) Entrance

A: When the value of the expression in the switch is the same as the value of the constant following A case, it is entered from that case

B: The default entry, when all cases are not satisfied, enter from default

(2) Export

A: When it comes to the end of the switch structure}, it ends naturally

B: Break the exit, break or return, etc

(3) Throughout

From the entrance to the exit, it will run through other cases and down until it reaches the exit

package com.dyy.exam;

import java.util.Scanner;
/* Since 1990, we have been fishing for three days and screening for two days
public class Exam9 {
    public static void main(String[] args) {
        int[] Pyear = new int[12];
        for (int i = 0; i < 12; i++) {
            switch (i + 1) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    Pyear[i] = 31;
                    break;
                case 2:
                    Pyear[i] = 28;
                    break;
                case 4:
                case 6:
                case 11:
                    Pyear[i] = 30;
                    break; }}int[] Ryear = Pyear;
        Ryear[1] = 29;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter year:");
        int year = scanner.nextInt();
        System.out.println("Please enter month:");
        int month = scanner.nextInt();
        System.out.println("Please enter date:");
        int day = scanner.nextInt();
        int total = 0;
        boolean flag = (year % 4= =0 && year % 100! =0) || year % 400= =0;
        for (int i = 1990; i < year; i++) {
            if (flag) {
                total += 366;
            } else {
                total += 365; }}for (int i = 0; i < month - 1; i++) {
            if (flag) {
                total += Ryear[i];
            } else{ total += Pyear[i]; }}int res = total % 5;
        if (res == 1 || res == 2 || res == 3) {
            System.out.print(year + "Year" + month + "Month" + day + "Day"
                    + "This day of fishing has passed since the first day."+total+"Day");
        } else {
            System.out.print(year + "Year" + month + "Month" + day + "Day"
                    + "This day to dry the net, since the first day passed."+total+"Day");package com.dyy.exam;


Copy the code