[Java process control learning] Select the structure

Choose a structure

if single selection structure

equals: Determine whether the string is equal, compare the string

//equals:判断字符串是否相等
if (s.equals("Hello")) {
    
    
    System.out.println(s);
}
System.out.println("End");
scanner.close();

if double selection structure

if( ) {
} else {
}

if (score >= 60) {
    
    
    System.out.println("成绩及格");
} else {
    
    
    System.out.println("成绩不及格");
}
scanner.close();

if multiple choice structure

if() {
} else if {
} else if {
} else {
}

if(score==100){
    
    
    System.out.println("Congratulations,full score!");
}else if(score<100 && score>=90){
    
    
    System.out.println("A级");
}else if(score<90 && score>=80){
    
    
    System.out.println("B级");
}else if(score<80 && score>=70){
    
    
    System.out.println("C级");
}else if(score<70 && score>=60){
    
    
    System.out.println("D级");
}else if(score<60) {
    
    
    System.out.println("不及格");
}else {
    
    
    System.out.println("input error!");
}
scanner.close();

/* The if statement has at most one else statement, and the else statement is after all else if statements.

The if statement can have several else if statements. They must be before the else statement.

Once one of the else if statements is detected as true, the other else if and else statements will be skipped.

*/

Nested if structure

  if(score<100){
    
    
        if(score<90){
    
    
            if (score<80){
    
    
                if (score<70){
    
    
                    if (score<60){
    
    
                        System.out.println("不及格");
                    }else{
    
    
                        System.out.println("D级");
                    }
                }else{
    
    
                    System.out.println("C级");
                }
            }else{
    
    
                System.out.println("B级");
            }
        }else{
    
    
            System.out.println("A级");

        }

    }else if(score==100){
    
    
        System.out.println("Congratulations,full score!");
    }else{
    
    
        System.out.println("input error!");
    }
    scanner.close();
}

switch multiple selection structure

The switch case statement determines whether a variable is equal to a certain value in a series of values, and each value is called a branch.

Variables can be:

  • byte, short, int, or char.

  • Starting from javaSE 7, switch supports String type

  • At the same time, the case label must be a string constant or a literal.

    switch (expression) {

    ​ case value :

    ​ //Statement

    ​ break;//optional

    ​ case value:

    ​ //Statement

    ​ break;//optional

    //You can have any number of case statements

    default://optional

    ​ //Statement

    }

//case穿透现象,末尾如果不加break;会从该语句一直执行完
//switch匹配一个具体的值

String code = "200";

switch (code){
    
    
    case"200":
    case"201":
    case"202":
        System.out.println("成功");
        break;
    case"401":
        System.out.println("资源未发现");
        break;
    case "500":
        System.out.println("未知错误");
        break;
    default:
        System.out.println("未知错误");
        break;
        //200,201,202都会输出"成功",利用穿透特性
} 
important point:

1. Generally, break will be added after each case;

2. Only constants can be connected after case;

3. The break should be followed by default; avoid adding statements afterwards;

Guess you like

Origin blog.csdn.net/weixin_44302662/article/details/114156147