第05课:条件判断
本课目标:掌握if、if-else、if-else if-else、嵌套if和switch语句的使用。
一、概念讲解
1.1 条件判断结构
┌─────────────────────────────────────────────┐ │ 条件判断结构 │ ├──────────────┬──────────────────────────────┤ │ if │ 单分支:条件为true执行 │ ├──────────────┼──────────────────────────────┤ │ if-else │ 双分支:条件为true/false分别执行│ ├──────────────┼──────────────────────────────┤ │ if-else if │ 多分支:多个条件判断 │ ├──────────────┼──────────────────────────────┤ │ switch │ 多值匹配:用于离散值判断 │ └──────────────┴──────────────────────────────┘1.2 if语句流程
┌───────────┐ │ 条件判断 │ └─────┬─────┘ │ ┌────────┴────────┐ │ │ [true] [false] │ │ ┌────┴────┐ │ │ 执行代码 │ │ └────┬────┘ │ │ │ └────────┬────────┘ │ ┌─────┴─────┐ │ 继续执行 │ └───────────┘1.3 switch语句结构
┌─────────────────────────────┐ │ switch(表达式) │ ├───────┬─────────┬───────────┤ │ case1 │ case2 │ case3 │ │ 执行1 │ 执行2 │ 执行3 │ ├───────┴─────────┴───────────┤ │ default(默认处理) │ └─────────────────────────────┘1.4 switch vs if
| 场景 | 推荐使用 |
|---|---|
| 判断范围(>、<、>=、<=) | if |
| 等值判断(==) | switch(case较多时) |
| 复杂条件组合 | if |
| 多个固定值匹配 | switch |
| JDK 14+:模式匹配 | switch增强版 |
二、语法格式
2.1 if语句
// 单分支if(条件){// 条件为true时执行}// 双分支if(条件){// 条件为true时执行}else{// 条件为false时执行}// 多分支if(条件1){// 条件1为true}elseif(条件2){// 条件2为true}elseif(条件3){// 条件3为true}else{// 以上条件都不满足}2.2 switch语句
// 经典switchswitch(表达式){case值1:// 执行代码1break;case值2:// 执行代码2break;case值3:case值4:// 值3和值4共享执行代码break;default:// 默认执行break;}// JDK 14+ 增强switchswitch(表达式){case值1->执行代码1;case值2,值3->执行代码2;default->默认执行;}三、代码案例
案例1:成绩等级计算器
importjava.util.Scanner;publicclassGradeCalculator{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.print("请输入成绩(0-100): ");intscore=scanner.nextInt();// 输入验证if(score<0||score>100){System.out.println("输入的成绩无效!请输入0-100之间的数。");}else{// 确定等级Stringgrade;Stringcomment;if(score>=90){grade="A";comment="优秀!继续保持!";}elseif(score>=80){grade="B";comment="良好,再接再厉!";}elseif(score>=70){grade="C";comment="中等,还有提升空间。";}elseif(score>=60){grade="D";comment="及格,需要努力了。";}else{grade="F";comment="不及格,请加油!";}System.out.println("\n===== 成绩报告 =====");System.out.println("成绩: "+score+"分");System.out.println("等级: "+grade);System.out.println("评语: "+comment);}scanner.close();}}案例2:闰年判断
importjava.util.Scanner;publicclassLeapYearChecker{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.print("请输入年份: ");intyear=scanner.nextInt();booleanisLeapYear;// 闰年规则:// 1. 能被4整除但不能被100整除// 2. 能被400整除if(year%400==0){isLeapYear=true;}elseif(year%100==0){isLeapYear=false;}elseif(year%4==0){isLeapYear=true;}else{isLeapYear=false;}// 输出结果if(isLeapYear){System.out.println(year+"年是闰年");System.out.println("2月有29天");}else{System.out.println(year+"年不是闰年");System.out.println("2月有28天");}// 验证:2000年是闰年,1900年不是闰年System.out.println("\n===== 验证 =====");System.out.println("2000年: "+(2000%400==0?"闰年":"平年"));System.out.println("1900年: "+(1900%400==0?"闰年":(1900%100==0?"平年":(1900%4==0?"闰年":"平年"))));scanner.close();}}案例3:简易计算器(带菜单)
importjava.util.Scanner;publicclassMenuCalculator{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);booleanrunning=true;while(running){System.out.println("\n===== 简易计算器 =====");System.out.println("1. 加法");System.out.println("2. 减法");System.out.println("3. 乘法");System.out.println("4. 除法");System.out.println("0. 退出");System.out.print("请选择操作: ");intchoice=scanner.nextInt();if(choice==0){System.out.println("感谢使用,再见!");running=false;}elseif(choice>=1&&choice<=4){System.out.print("请输入第一个数: ");doublenum1=scanner.nextDouble();System.out.print("请输入第二个数: ");doublenum2=scanner.nextDouble();doubleresult;Stringoperator;switch(choice){case1:result=num1+num2;operator="+";break;case2:result=num1-num2;operator="-";break;case3:result=num1*num2;operator="×";break;case4:if(num2==0){System.out.println("错误:除数不能为0!");continue;}result=num1/num2;operator="÷";break;default:System.out.println("无效选择!");continue;}System.out.printf("%.2f %s %.2f = %.2f%n",num1,operator,num2,result);}else{System.out.println("无效选择,请重新输入!");}}scanner.close();}}四、常见错误
错误1:if条件中使用赋值=
// 错误示例intx=5;if(x=10){// 编译错误!=是赋值,结果是int,不能转boolean// ...}// 正确写法if(x==10){// ...}错误2:缺少大括号
// 不推荐(容易出错)if(score>=60)System.out.println("及格");elseSystem.out.println("不及格");// 推荐写法(始终使用大括号)if(score>=60){System.out.println("及格");}else{System.out.println("不及格");}错误3:switch忘记break
// 错误示例:缺少break会导致"穿透"switch(choice){case1:System.out.println("加法");// 忘记break!case2:System.out.println("减法");// choice=1时也会执行这里break;}// 正确写法switch(choice){case1:System.out.println("加法");break;case2:System.out.println("减法");break;}错误4:switch中使用浮点数
// 错误:switch不支持float/doubledoubled=3.14;switch(d){// 编译错误!case1.0:break;}// 正确:switch只支持整数、字符、字符串、枚举inti=3;switch(i){case3:break;}五、课后练习
练习1(基础)
编写程序,输入三个整数,输出其中最大的数。要求使用if语句。
练习2(进阶)
编写程序,输入月份(1-12),输出该月有多少天(假设非闰年)。使用switch语句实现。
练习3(思考题)
编写程序,输入年、月、日,判断这一天是该年的第几天。
六、本课小结
| 语句 | 用途 | 注意事项 |
|---|---|---|
| if | 单分支判断 | 条件必须是boolean |
| if-else | 双分支判断 | else匹配最近的未匹配if |
| if-else if | 多分支判断 | 从上到下依次判断 |
| switch | 离散值匹配 | 注意break,支持int/char/String |
| 嵌套if | 复杂条件 | 不超过3层为宜 |
本课程持续更新中,欢迎关注!