顺序结构
顺序结构比较简单,按照代码书写的顺序一行一行执行。
System.out.println("aaa"); System.out.println("bbb"); System.out.println("ccc"); // 运行结果 aaa bbb ccc
如果调整代码的书写顺序, 则执行顺序也发生变化
System.out.println("aaa"); System.out.println("ccc"); System.out.println("bbb"); // 运行结果 aaa ccc bbb
分支结构
if 语句
语法格式1
if(布尔表达式){ // 语句 }
如果布尔表达式结果为true,执行if中的语句,否则不执行。
比如:小明,如果这次考试考到90分或以上,给你奖励一个鸡腿。
int score = 92; if(score >= 90){ System.out.println("吃个大鸡腿!!!"); }
语法格式2
if(布尔表达式){ // 语句1 }else { // 语句2 }
如果布尔表达式结果为true,则执行if中语句,否则执行else中语句。
比如:小明,如果这次考到90分以上,给你奖励一个大鸡腿,否则奖你一个大嘴巴子
int score = 92; if(score >= 90){ System.out.println("吃个大鸡腿!!!"); }else{ System.out.println("挨大嘴巴子!!!"); }
语法格式3
if(布尔表达式1) { // 语句1 }else if(布尔表达式2) { // 语句2 }else { // 语句3 }
表达式1成立,执行语句1,否则表达式2成立,执行语句2,否则执行语句3
比如:考虑到学生自尊,不公开分数排名,因此:
分数在 [90, 100] 之间的,为优秀
分数在 [80, 90) 之前的,为良好
分数在 [70, 80) 之间的,为中等
分数在 [60, 70) 之间的,为及格
分数在 [ 0, 60) 之间的,为不及格
错误数据
按照上述办法通知学生成绩,代码如下
if(score >= 90){ System.out.println("优秀"); }else if(score >= 80 && score < 90){ System.out.println("良好"); }else if(score >= 70 && score < 80){ System.out.println("中等"); }else if(score >= 60 && score < 70){ System.out.println("及格"); }else if(score >= 0 && score < 60){ System.out.println("不及格"); }else{ System.out.println("错误数据"); }
练习
练习一
判断一个数字是奇数还是偶数
int num = 10; if (num % 2 == 0) { System.out.println("num 是偶数"); } else { System.out.println("num 是奇数"); }
练习二
判断一个数字是正数,负数,还是零
int num = 10; if (num > 0) { System.out.println("正数"); } else if (num < 0) { System.out.println("负数"); } else { System.out.println("0"); }
练习三
判断一个年份是否为闰年
int year = 2000; if (year % 100 == 0) { // 判定世纪闰年 if (year % 400 == 0) { System.out.println("是闰年"); } else { System.out.println("不是闰年"); } } else { // 普通闰年 if (year % 4 == 0) { System.out.println("是闰年"); } else { System.out.println("不是闰年"); } }
注意事项
代码风格
风格1-----> 推荐
int x = 10; if (x == 10) { // 语句1 } else { // 语句2 }
风格2
int x = 10; if (x == 10) { // 语句1 } else { // 语句2 }
虽然两种方式都是合法的, 但是 Java 中更推荐使用风格1, { 放在 if / else 同一行. 代码跟紧凑
分号问题
int x = 20; if (x == 10); { System.out.println("hehe"); } // 运行结果 hehe
此处多写了一个 分号, 导致分号成为了 if 语句的语句体, 而 { } 中的代码已经成为了和一个 if 无关的代码块
悬垂 else 问题
int x = 10; int y = 10; if (x == 10) if (y == 10) System.out.println("aaa"); else System.out.println("bbb");
if / else 语句中可以不加 大括号 . 但是也可以写语句(只能写一条语句). 此时 else 是和最接近的 if 匹配.但是实际开发中我们 不建议 这么写.,最好加上大括号。
switch 语句
基本语法
switch(表达式){ case 常量值1:{ 语句1; [break;] } case 常量值2:{ 语句2; [break;] } ... default:{ 内容都不满足时执行语句; [break;] } }
执行流程
1. 先计算表达式的值
2. 和case依次比较,一旦有响应的匹配就执行该项下的语句,直到遇到break时结束
3. 当表达式的值没有与所列项匹配时,执行default
代码示例
根据 day 的值输出星期
int day = 1; switch(day) { case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; case 3: System.out.println("星期三"); break; case 4: System.out.println("星期四"); break; case 5: System.out.println("星期五"); break; case 6: System.out.println("星期六"); break; case 7: System.out.println("星期日"); break; default: System.out.println("输入有误"); break; }
注意事项
1、多个case后的常量值不可以重复
2、switch的括号内只能是以下类型的表达式
基本类型:byte、char、short、int,注意不能是long类型
引用类型:String常量串、枚举类型
double num = 1.0; switch(num) { case 1.0: System.out.println("hehe"); break; case 2.0: System.out.println("haha"); break; } // 编译出错 Test.java:4: 错误: 不兼容的类型: 从double转换到int可能会有损失 switch(num) { ^ 1 个错误
3、break 不要遗漏, 否则会失去 "多分支选择" 的效果
int day = 1; switch(day) { case 1: System.out.println("星期一"); // break; case 2: System.out.println("星期二"); break; } // 运行结果 星期一 星期二
4、switch 不能表达复杂的条件
// 例如: 如果 num 的值在 10 到 20 之间, 就打印 hehe // 这样的代码使用 if 很容易表达, 但是使用 switch 就无法表示. if (num > 10 && num < 20) { System.out.println("hehe"); }
5、switch 虽然支持嵌套, 但是很丑,一般不推荐
int x = 1; int y = 1; switch(x) { case 1: switch(y) { case 1: System.out.println("hehe"); break; } break; case 2: System.out.println("haha"); break; }
代码的美观程度也是一个重要的标准. 毕竟这是看脸的世界
综上, 我们发现, switch 的使用局限性是比较大的