Java分支结构

117 阅读3分钟
1. 三种结构
三种结构:任何复杂的程序逻辑,都可以通过三种控制结构来实现
     ▶ 顺序结构:从上到下依次线性执行每一条语句,没有跳转或分支的执行完程序
     ▶ 分支结构:根据指定条件执行不同的代码块,并非每条语句都执行
     ▶ 循环结构:以重复执行一段代码块,直到满足特定条件才停止语句的执行
2. 分支结构
Java分支语句:Java中有 if 和 switch 语句
(1)if语句

语法:if(boolean条件语句) {语句块}

  • 格式
if (条件) {
    // 满足条件时执行的代码块
}
// 不满足以上条件时执行的代码块
  • 执行流程图
flowchat
st=>start: 开始
op=>operation: 程序主体
cond=>condition: if关系表达式
io=>inputoutput: 语句体
e=>end: 执行结束

st(right)->op(right)->cond
cond(yes)->io(bottom)->e
cond(no)->e
st@>op({"stroke":"cyan","stroke-width":3,"arrow-end":"classic-wide-long"})@>cond({"stroke":"cyan","stroke-width":3,"arrow-end":"classic-wide-long"})@>e({"stroke":"Red","stroke-width":6,"arrow-end":"classic-wide-long"})
st@>cond({"stroke":"Green","stroke-width":3,"arrow-end":"classic-wide-long"})@>io({"stroke":"#C0FF3E","stroke-width":6,"arrow-end":"classic-wide-long"})@>e({"stroke":"#66CD00","stroke-width":3,"arrow-end":"classic-wide-long"})
  • 代码演示
package bases;

public class Demo04_If {
    /**
     * 分支语句
     *
     * @param args
     */
    public static void main(String[] args) {
        /*
         * if语句
         */
        // 打折
        double price = 300.0;
        if (price >= 500) {
            //满500则打八折
            price *= 0.8;
        }
        System.out.println("最终消费金额为:" + price);

        //判断成绩的合法性
        int score = 80;
        if (score >= 0 && score <= 100) {
            System.out.println("成绩合法");
        }
        System.out.println("成绩不合法,请继续...");
    }
}

在这里插入图片描述

(2)if……else 语句

语法:if(boolean条件语句){语句块1} else{语句块2}

  • 格式
if (条件) {
    // 条件为真时执行的代码块
} else {
    // 条件为假时执行的代码块
}
  • 执行流程图
flowchat
st=>start: 开始
op=>operation: 程序主体
cond=>condition: if关系表达式
io=>inputoutput: 语句体1
io1=>inputoutput: 语句体2
sub1=>subroutine: else语句
e=>end: 执行结束

st(right)->op(right)->cond
cond(yes)->io(bottom)->e
cond(no)->sub1->io1->e

st@>op({"stroke":"cyan","stroke-width":3,"arrow-end":"classic-wide-long"})@>cond({"stroke":"cyan","stroke-width":3,"arrow-end":"classic-wide-long"})@>e({"stroke":"Red","stroke-width":6,"arrow-end":"classic-wide-long"})
st@>cond({"stroke":"Green","stroke-width":3,"arrow-end":"classic-wide-long"})@>io({"stroke":"#C0FF3E","stroke-width":6,"arrow-end":"classic-wide-long"})@>e({"stroke":"#66CD00","stroke-width":3,"arrow-end":"classic-wide-long"})
st@>cond({"stroke":"Green","stroke-width":3,"arrow-end":"classic-wide-long"})@>sub1({"stroke":"red","stroke-width":6,"arrow-end":"classic-wide-long"})@>io1({"stroke":"#66CD00","stroke-width":3,"arrow-end":"classic-wide-long"})@>e({"stroke":"#66CD00","stroke-width":3,"arrow-end":"classic-wide-long"})
  • 代码演示
package bases;

public class Demo04_If {
    /**
     * 分支语句
     *
     * @param args
     */
    public static void main(String[] args) {
        /*
         * if……else语句
         */
        //满500打8折,不满500打9折:
        double price = 300.0;
        if (price >= 500) {
            price *= 0.8;
        } else {
            price *= 0.9;
        }
        System.out.println("最终消费金额为:" + price);
        //判断成绩合法还是不合法
        int score = 95;
        if (score >= 0 && score <= 100) {
            System.out.println(score + " 合法成绩");
        } else {
            System.out.println(score + " 不合法成绩");
        }
    }
}

在这里插入图片描述

(3)if……else…if 语句

语法:if(boolean条件语句1){语句块1} else if(boolean条件语句2){语句块2} else{语句块3}

  • 格式
   if (条件1) {
       // 如果条件1为真,则执行这里的代码
   } else if (条件2) {
       // 如果条件2为真,则执行这里的代码
   } else {
       // 如果以上条件都不满足,则执行这里的代码
   }
  • 执行流程图
flowchat
st=>start: 开始
op=>operation: 程序主体
cond=>condition: if关系表达式1
io=>inputoutput: 语句体1
cond1=>condition: else if关系表达式2
io1=>inputoutput: 语句体2
sub1=>subroutine: else语句
io2=>inputoutput: 语句体3
e=>end: 执行结束

st(right)->op(right)->cond
cond(yes)->io(bottom)->e
cond(no)->cond1
cond1(yes)->io1->e
cond1(no)->sub1->io2->e

st@>op({"stroke":"cyan","stroke-width":3,"arrow-end":"classic-wide-long"})@>cond({"stroke":"cyan","stroke-width":3,"arrow-end":"classic-wide-long"})@>e({"stroke":"Red","stroke-width":6,"arrow-end":"classic-wide-long"})
st@>cond({"stroke":"red","stroke-width":3,"arrow-end":"classic-wide-long"})@>cond1({"stroke":"red","stroke-width":6,"arrow-end":"classic-wide-long"})@>sub1({"stroke":"red","stroke-width":6,"arrow-end":"classic-wide-long"})@>io2({"stroke":"#66CD00","stroke-width":3,"arrow-end":"classic-wide-long"})@>e({"stroke":"#66CD00","stroke-width":3,"arrow-end":"classic-wide-long"})
st@>cond({"stroke":"Green","stroke-width":3,"arrow-end":"classic-wide-long"})@>io({"stroke":"green","stroke-width":6,"arrow-end":"classic-wide-long"})@>e({"stroke":"#66CD00","stroke-width":3,"arrow-end":"classic-wide-long"})
st@>cond1({"stroke":"green","stroke-width":6,"arrow-end":"classic-wide-long"})@>io1({"stroke":"green","stroke-width":6,"arrow-end":"classic-wide-long"})
  • 代码演示
package bases;

public class Demo04_If {
    /**
     * 分支语句
     *
     * @param args
     */
    public static void main(String[] args) {
        /*
         * if……else…if语句
         */
        //满2000打5折,满1000不满2000打7折,满500不满1000打8折,不满500打9折:
        double price = 6000.0;
        if (price >= 2000) {
            price *= 0.5;
        } else if (price >= 1000) {
            price *= 0.7;
        } else if (price >= 500) {
            price *= 0.8;
        } else {
            price *= 0.9;
        }
        System.out.println("最终消费金额为:" + price);
    }
}

在这里插入图片描述

3. switch分支语句
Switch语句:一种用于多分支条件判断的控制流语句(根据一个表达式的值,匹配相应的分支来执行相应的代码块)
     ▶ 优点:效率高,结构清晰,代码易读
  • 格式
switch (整数、字符、枚举或字符串类型等) {
    case1:
        // 执行语句块1
        break;
    case2:
        // 执行语句块2
        break;
    ...
    case 值n:
        // 执行语句块n
        break;
    default:
        // 默认语句块
        break;
}

注:==如果某个case分支的代码块没有break语句,将会继续执行下一个case分支的代码,称为"case穿透",如果所有case都不匹配,则会执行default语句块(可选)==

  • 执行流程图
graph LR
    A[开始] --> B{Switch条件}
    B -- case 1 --> C[执行语句块1]
    B -- case 2 --> D[执行语句块2]
    B -- case 3 --> E[执行语句块3]
    B -- default --> F[默认语句块]
    C --> G[结束]
    D --> G
    E --> G
    F --> G
  • 代码演示
package bases;

import java.util.Scanner;

public class Demo04_Switch {
    public static void main(String[] args) {
        /*
         * switch分支语句
         */
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户命令:");
        int command = scanner.nextInt();
        System.out.println("用户指令:" + command);


        // 案例:输入数字转换为星期
        System.out.print("请输入需要转换的数字:");
        int day = scanner.nextInt();
        String dayName;

        switch (day) {
            case 1:
                dayName = "星期一";
                break;
            case 2:
                dayName = "星期二";
                break;
            case 3:
                dayName = "星期三";
                break;
            case 4:
                dayName = "星期四";
                break;
            case 5:
                dayName = "星期五";
                break;
            case 6:
                dayName = "星期六";
                break;
            case 7:
                dayName = "星期日";
                break;
            default:
                dayName = "无效的日期";
                break;
        }
        System.out.println("您输入的数字为:" + day + "  转换为星期数为:" + dayName);
    }
}