Java flow control 03 (sequence structure and selection structure) (decompiled with IDEA)

Sequence structure *
1. The basic structure of Java is sequence structure. Unless otherwise specified, code examples will be executed sentence by sentence in order

package com.hao.struct;

public class ShunXuDemo {
    public static void main(String[] args) {
        System.out.println("Helloworld1");
        System.out.println("Helloworld2");
        System.out.println("Helloworld3");
        System.out.println("Helloworld4");
        System.out.println("Helloworld5");
    }
}

Output example
Insert picture description here
selection structure *
if single-selection structure
Syntax:
if(Boolean expression){ //Statement that will be executed if the Boolean expression is True } Code example


package com.hao.struct;

import java.util.Scanner;

public class ifDemo01 {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入内容:");
        String str =scanner.nextLine();
        //equals:判断字符串是否相等
        if(str.equals("Hello")){
            System.out.println(str);
        }
        System.out.println("End");
        scanner.close();
    }
}

Sample output
Insert picture description here

if double selection structure
Syntax:
if(Boolean expression){ //The statement that will be executed if the Boolean expression is True }else{ // The statement that will be executed if the Boolean expression is false } Code example




package com.hao.struct;

import java.util.Scanner;

public class ifDemo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //考试分数大于60分就是及格,小于60分就是不及格。
        System.out.println("请输入成绩");
        int score = scanner.nextInt();
        if(score>60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }
        scanner.close();
    }
}

Output example
Insert picture description here
if multiple choice structure
Syntax:
if(Boolean expression){ //The statement that will be executed if the Boolean expression is true }else if(Boolean expression 2){ //The statement that will be executed if the Boolean expression 2 is true }else{ //Statement that will be executed if the boolean expression is false } Code example






package com.hao.struct;

import java.util.Scanner;

import java.util.Scanner;

public class ifDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //考试分数大于60分就是及格,小于60分就是不及格。
        System.out.println("请输入成绩");
        int score = scanner.nextInt();
        if(score==100){
            System.out.println("满分!");
        }else if(score<100&&score>=90){
            System.out.println("优");
        }else if(score<90&&score>=70){
            System.out.println("良");
        }else if(score<70&&score>=60){
            System.out.println("及格");
        } else{
            System.out.println("不及格");
        }
        scanner.close();
    }
}

Output example
Insert picture description here
switch multi-select structure
1. The switch case statement judges whether a variable is equal to a value in a series, and each value becomes a branch
2. The variable type in the switch statement can be:
1) byte, short, int, Or char
2) Starting from Java SE7, swtich supports the string String type, and the case label must be a string constant or a literal
code example

package com.hao.struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade='C';
        //每个case后面要加上break;不然由于case穿透现象,会自动输出剩下case中的内容
        switch(grade){
            case'A':
                System.out.println("优秀");
                break;
            case'B':
                System.out.println("良好");
                break;
            case'C':
                System.out.println("及格");
                break;
            case'D':
                System.out.println("不及格");
                break;
            default:
                System.out.println("未知等级");
        }
    }
}

Output sample
Insert picture description here
code example

package com.hao.struct;


public class SwitchDemo02 {
    public static void main(String[] args) {
        String name="元浩";
        switch(name){
            case "元浩":
                System.out.println(name+"是本人没错了");
                break;
            case "小猪":
                System.out.println(name+"是个猪");
                break;
            default:
                System.out.println("不懂");
        }
    }
}

The output example is
Insert picture description here
decompiled with IDEA
1. Click Project Structure in the menu bar
Insert picture description here
2. Find the address of the class file
Insert picture description here
3. Find the address and open the class file you want to open, you can find the machine internal code that you can't understand
Insert picture description here
Insert picture description here
3. In IDEA Choose to open the folder where the Java file is located
Insert picture description here
Insert picture description here
4. Drag the class file you want to decompile to the corresponding folder
Insert picture description here
5. You can find that there is an extra class file in IDEA, double-click to open it
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/111239632