「Java教案」选择结构

82 阅读7分钟

课程目标

1. 知识目标

  1. 能够正确使用Java选择结构包括if、if-else、switch的语法规则与执行逻辑编写程序。
  2. 能够合理的在程序控制中的使用选择结构。

2 能力目标

  1. 能够根据需求使用选择结构设计代码,解决多条件分支问题。
  2. 弄过具备调试能力,调试选择结构代码中常见的错误。

3 思政目标

  1. 人生的选择好比“条件判断”,引导学生树立理性决策意识。
  2. 强调代码规范与严谨性,培养工匠精神。

教学内容

1.选择结构概述

  1. 选择结构是根据条件判断结果决定执行不同代码块的逻辑控制结构。
  2. 选择结构的类型包括:单分支选择结构(if)、双分支选择结构(if-else)、多分支选择结构(switch)。

2 语法规则与执行流程

  1. if语句:最基本的条件判断结构。
if (条件表达式) {

    // 条件为 true 时执行的代码

}
  1. if-else语句:在if基础上增加else分支。
if (条件表达式) {

    // 条件为 true 时执行的代码

} else {

    // 条件为 false 时执行的代码

}
  1. if-else if-else语句:用于多条件判断。
if (条件1) {

    // 条件1true 时执行的代码

} else if (条件2) {

    // 条件2true 时执行的代码

} else {

    // 所有条件都不满足时执行的代码

}
  1. switch语句:基于某个表达式的值的多分支选择。
switch (表达式) {

    case1:

        // 代码块1

        break;

    case2:

        // 代码块2

        break;

    // 可以有任意数量的 case 语句

    default:

        // 默认代码块

}

3 应用场景

用户登录验证、成绩等级划分、商品折扣计算等。

重点分析

难点分析

教学活动设计

概念引入

通过问题“如果明天下雨,你会带伞吗?否则呢?”,引出if-else结构。

1 选择结构基础

if语句:最基本的条件判断结构。

if (条件表达式) {

    // 条件为 true 时执行的代码

}

示例:

int age = 17;
if (age >= 18) {
    System.out.println("成年人");
} else {
    System.out.println("未成年人");
}

if-else语句:在if基础上增加else分支。

if (条件表达式) {

    // 条件为 true 时执行的代码

} else {

    // 条件为 false 时执行的代码

}

示例:

int age = 17;
if (age >= 18) {
    System.out.println("成年人");
} else {
    System.out.println("未成年人");
}

if-else if-else语句:用于多条件判断。

if (条件1) {

    // 条件1true 时执行的代码

} else if (条件2) {

    // 条件2true 时执行的代码

} else {

    // 所有条件都不满足时执行的代码

}

示例:

int score = 85;
if (score >= 90) {
    System.out.println("优秀");
} else if (score >= 80) {
    System.out.println("良好");
} else if (score >= 60) {
    System.out.println("及格");
} else {
    System.out.println("不及格");
}

switch语句:基于某个表达式的值的多分支选择。

switch (表达式) {

    case1:

        // 代码块1

        break;

    case2:

        // 代码块2

        break;

    // 可以有任意数量的 case 语句

    default:

        // 默认代码块

}

示例:

int day = 3;
switch (day) {
    case 1:
        System.out.println("星期一");
        break;
    case 2:
        System.out.println("星期二");
        break;
    case 3:
        System.out.println("星期三");
        break;
    // 其他 case...
    default:
        System.out.println("无效的日期");
}

2 嵌套与复杂逻辑

嵌套if-else:

if (score >= 90) {
    System.out.println("优秀");
} else if (score >= 80) {
    System.out.println("良好");
} else {
    System.out.println("需努力");
}

说明:避免过度嵌套(建议≤3层),可通过提前返回或拆分方法优化。

switch的case穿透:

int num = 2;
switch (num) {
    case 1: System.out.println("一");
    case 2: System.out.println("二"); // 无break,会继续执行case 3
    case 3: System.out.println("三"); break;
    default: System.out.println("其他");
}
// 输出:二 三

3 边界条件与防御性编程

浮点数比较:

double a = 0.1 + 0.2;
if (Math.abs(a - 0.3) < 0.0001) { // 允许微小误差
    System.out.println("相等");
}

案例解析

案例: 会员折扣计算

public class DiscountCalculator {
    public static void main(String[] args) {
        double price = 199.9;
        boolean isMember = true;
        if (isMember) {
            if (price > 200) {
                System.out.println("会员价:" + (price * 0.8));
            } else {
                System.out.println("会员价:" + (price * 0.9));
            }
        } else {
            System.out.println("原价:" + price);
        }
    }
}

运行结果

会员价:179.91

说明:Java源文件名保存为“DiscountCalculator.java”。通过嵌套if实现会员与非会员的分级折扣,强调逻辑层次。

案例: 成绩等级划分(switch)

public class GradeSwitch {
    public static void main(String[] args) {
        int score = 85;
        switch (score / 10) {
            case 10:
            case 9: System.out.println("A"); break;
            case 8: System.out.println("B"); break;
            default: System.out.println("C及以下");
        }
    }
}

运行结果

B

说明:Java源文件名保存为“GradeSwitch.java”。利用整数除法简化区间判断,演示case穿透与break的作用。

案例: 电商促销活动

需求:根据用户消费金额(amount)和会员等级(level)计算折扣:

普通会员(level=1):满100元享9折,满200元享8折。

高级会员(level=2):满100元享8折,满200元享7折。

public class Promotion {
    public static void main(String[] args) {
        double amount = 180;
        int level = 2; // 1-普通会员,2-高级会员
        double discount;
        if (level == 1) {
            if (amount >= 200) {
                discount = 0.8;
            } else if (amount >= 100) {
                discount = 0.9;
            } else {
                discount = 1.0;
            }
        } else if (level == 2) {
            if (amount >= 200) {
                discount = 0.7;
            } else if (amount >= 100) {
                discount = 0.8;
            } else {
                discount = 1.0;
            }
        } else {
            discount = 1.0; // 非会员无折扣
        }
        System.out.println("实付金额:" + (amount * discount));
    }
}

运行结果

实付金额:144.0

说明:Java源文件名保存为“Promotion.java”。

案例: 学生成绩管理系统(switch优化)

需求:根据用户输入的命令执行操作:

1:查询成绩

2:修改成绩

3:退出系统

public class GradeSystem {
    public static void main(String[] args) {
        String command = "2";
        switch (command) {
            case "1":
                System.out.println("查询成绩功能");
                break;
            case "2":
                System.out.println("修改成绩功能");
                break;
            case "3":
                System.out.println("系统退出");
                break;
            default:
                System.out.println("无效命令");
        }
    }
}

运行结果

修改成绩功能

说明:Java源文件名保存为“GradeSystem.java”。

常见错误

错误: if (x = 5)(赋值而非比较)

解决方法: 强调==与=的区别,推荐使用Yoda条件(5 == x)避免笔误。

错误: switch中遗漏break导致逻辑错误。

解决方法: 通过代码对比展示穿透效果,要求所有case后必须添加注释说明是否需要break。

课堂练习

练习: 编写程序判断输入的年份是否为闰年(能被4整除但不能被100整除,或能被400整除)。

public class LeapYear {
    public static void main(String[] args) {
        int year = 2025;
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            System.out.println("闰年");
        } else {
            System.out.println("平年");
        }
    }
}

运行结果

平年

说明:Java源文件名保存为“LeapYear.java”。

课后作业

作业: 设计一个ATM取款程序,根据输入金额判断是否允许取款(如余额≥100且≤5000)。

public class ATM {
    public static void main(String[] args) {
        double balance = 2000;
        double withdraw = 1500;
        if (withdraw >= 100 && withdraw <= 5000 && withdraw <= balance) {
            System.out.println("取款成功,余额:" + (balance - withdraw));
        } else {
            System.out.println("取款失败");
        }
    }
}

运行结果

取款成功,余额:500.0

说明:Java源文件名保存为“ATM.java”。

考核设计

1 过程性考核(40%)

  1. 课堂练习的完成程度(20%)
  2. 编写代码的规范性与添加注释的规范性(10%)
  3. 参与小组讨论和解决问题的能力(10%)

2 终结性考核(60%)

  1. 理论测试(20%)
  2. 综合项目(40%)

理论测试:

选择题:switch语句中,若某个case后缺少break,会发生什么?

A.编译错误    B.仅执行该case代码    C.穿透执行后续case代码(正确)    D.程序终止

答案:D

填空题:判断闰年的条件是:(year % 4 == 0 && year % 100 != 0) || ________

答案:year % 400 == 0

简答题:switch中default的作用是什么?是否必须?

答案:在Java的switch语句中,default关键字用于定义一个默认执行的代码块,当所有case条件都不匹配时,程序会执行default部分的代码。default在switch语句中不是必须的,它是可选的。

综合项目:

编程题:编写Java程序,根据输入的月份输出对应季节(3-5月春季,6-8月夏季,依此类推)。

public class SeasonFinder {
    public static void main(String[] args) {
        int month = 11;

        String season;

        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "冬季";
                break;
            case 3:
            case 4:
            case 5:
                season = "春季";
                break;
            case 6:
            case 7:
            case 8:
                season = "夏季";
                break;
            case 9:
            case 10:
            case 11:
                season = "秋季";
                break;
            default:
                season = "无效的月份";
        }

        System.out.println(month + "月是" + season);
    }
}

运行结果

11月是秋季

说明:Java源文件名保存为“SeasonFinder.java”。

编程题:编写Java程序,设计一个“密码强度检测器”,根据以下规则输出强度等级:

  1. 长度≥8且包含大小写字母和数字:强
  2. 长度≥6且包含字母或数字:中
  3. 其他:弱
public class PasswordChecker {
    public static void main(String[] args) {
        String password = "123456";
        boolean hasUpper = false, hasLower = false, hasDigit = false;
        for (char c : password.toCharArray()) {
            if (Character.isUpperCase(c)) hasUpper = true;
            else if (Character.isLowerCase(c)) hasLower = true;
            else if (Character.isDigit(c)) hasDigit = true;
        }
        if (password.length() >= 8 && hasUpper && hasLower && hasDigit) {
            System.out.println("强");
        } else if (password.length() >= 6 && (hasUpper || hasLower || hasDigit)) {
            System.out.println("中");
        } else {
            System.out.println("弱");
        }
    }
}

运行结果

说明:Java源文件名保存为“PasswordChecker.java”。