资讯动态

第05课-条件判断

发布时间:2026/9/6 13:13:31 来源:尧图企业网站定制
第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等值判断switchcase较多时复杂条件组合if多个固定值匹配switchJDK 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){ScannerscannernewScanner(System.in);System.out.print(请输入成绩(0-100): );intscorescanner.nextInt();// 输入验证if(score0||score100){System.out.println(输入的成绩无效请输入0-100之间的数。);}else{// 确定等级Stringgrade;Stringcomment;if(score90){gradeA;comment优秀继续保持;}elseif(score80){gradeB;comment良好再接再厉;}elseif(score70){gradeC;comment中等还有提升空间。;}elseif(score60){gradeD;comment及格需要努力了。;}else{gradeF;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){ScannerscannernewScanner(System.in);System.out.print(请输入年份: );intyearscanner.nextInt();booleanisLeapYear;// 闰年规则// 1. 能被4整除但不能被100整除// 2. 能被400整除if(year%4000){isLeapYeartrue;}elseif(year%1000){isLeapYearfalse;}elseif(year%40){isLeapYeartrue;}else{isLeapYearfalse;}// 输出结果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%4000?闰年:平年));System.out.println(1900年: (1900%4000?闰年:(1900%1000?平年:(1900%40?闰年:平年))));scanner.close();}}案例3简易计算器带菜单importjava.util.Scanner;publicclassMenuCalculator{publicstaticvoidmain(String[]args){ScannerscannernewScanner(System.in);booleanrunningtrue;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(请选择操作: );intchoicescanner.nextInt();if(choice0){System.out.println(感谢使用再见);runningfalse;}elseif(choice1choice4){System.out.print(请输入第一个数: );doublenum1scanner.nextDouble();System.out.print(请输入第二个数: );doublenum2scanner.nextDouble();doubleresult;Stringoperator;switch(choice){case1:resultnum1num2;operator;break;case2:resultnum1-num2;operator-;break;case3:resultnum1*num2;operator×;break;case4:if(num20){System.out.println(错误除数不能为0);continue;}resultnum1/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();}}四、常见错误错误1if条件中使用赋值// 错误示例intx5;if(x10){// 编译错误是赋值结果是int不能转boolean// ...}// 正确写法if(x10){// ...}错误2缺少大括号// 不推荐容易出错if(score60)System.out.println(及格);elseSystem.out.println(不及格);// 推荐写法始终使用大括号if(score60){System.out.println(及格);}else{System.out.println(不及格);}错误3switch忘记break// 错误示例缺少break会导致穿透switch(choice){case1:System.out.println(加法);// 忘记breakcase2:System.out.println(减法);// choice1时也会执行这里break;}// 正确写法switch(choice){case1:System.out.println(加法);break;case2:System.out.println(减法);break;}错误4switch中使用浮点数// 错误switch不支持float/doubledoubled3.14;switch(d){// 编译错误case1.0:break;}// 正确switch只支持整数、字符、字符串、枚举inti3;switch(i){case3:break;}五、课后练习练习1基础编写程序输入三个整数输出其中最大的数。要求使用if语句。练习2进阶编写程序输入月份(1-12)输出该月有多少天假设非闰年。使用switch语句实现。练习3思考题编写程序输入年、月、日判断这一天是该年的第几天。六、本课小结语句用途注意事项if单分支判断条件必须是booleanif-else双分支判断else匹配最近的未匹配ifif-else if多分支判断从上到下依次判断switch离散值匹配注意break支持int/char/String嵌套if复杂条件不超过3层为宜本课程持续更新中欢迎关注

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价