The first stage: JAVA Quick Start (forty-ninth lesson: JAVA_if-else if-else structure multiple choice)

grammar structure:

if(布尔表达式1) {

语句块1;

} else if(布尔表达式2) {

语句块2;

}……

else if(布尔表达式n){

    语句块n;

} else {

    语句块n+1;

}

      When a Boolean expression is true, a block statement is executed; otherwise, determining a Boolean expression 2, when the Boolean expression 2 is true, execute statement blocks 2; otherwise, continue to determine the Boolean expression 3 ······ ; if 1 ~ n boolean expressions are determined as false, the statements block n + 1, i.e. the else part. Flow chart shown in Figure 3-7.

1.png

FIGS. 3-7 if-else if-else flowchart multiple selection structure

[Example 3-5] if-else if-else multiple selection structure

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public class Test5 {

    public static void main(String[] args) {

        int age = (int) (100 * Math.random());

        System.out.print("年龄是" + age + ", 属于");

        if (age < 15) {

            System.out.println("儿童, 喜欢玩!");

        } else if (age < 25) {

            System.out.println("青年, 要学习!");

        } else if (age < 45) {

            System.out.println("中年, 要工作!");

        } else if (age < 65) {

            System.out.println("中老年, 要补钙!");

        } else if (age < 85) {

            System.out.println("老年, 多运动!");

        } else {

            System.out.println("老寿星, 古来稀!");

        }

    }

}

 

 Figure 3-8 Example 3-5 FIG 1 operating results

 

 

Published 49 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/ZGL_cyy/article/details/104092761