2019年6月24日星期一
16时26分48秒
第9节选择结构-if语句-switch语句
目录
1_1_9_01_流程概述与顺序结构 2
1_1_9_02_选择结构_单if语句 2
1_1_9_03_选择结构_标准if-else语句 2
1_1_9_04_选择结构_扩展if-else语句 2
1_1_9_05_练习_用if语句实现考试成 2
1_1_9_06_练习_用if语句替换三元运 2
1_1_9_07_选择结构_标准的switch 3
1_1_9_08_选择结构_穿透的switch 3
1_1_9_01_流程概述与顺序结构
流程图
// 顺序结构
public class Demo01Sequence { //sequence 顺序
public static void main(String[] args) {
System.out.println("今天天气不错");
System.out.println("挺风和日丽的");
System.out.println("我们下午没课");
System.out.println("这的确挺爽的");
}
}
1_1_9_02_选择结构_单if语句
public class Demo02If {
public static void main(String[] args) {
System.out.println("今天天气不错,这种压马路.......突然发现一个快乐的地方: 网吧");
int age = 16;
if (age >= 18){
System.out.println("进入网吧,开始high!");
System.out.println("遇到了一群猪队友,开始骂街。");
System.out.println("感觉不爽,结账走人。");
}
System.out.println("回家吃饭");
}
}
public
class Demo02If {
public static void main(String[] args) {
System.out.println("今天天气不错,这种压马路.......突然发现一个快乐的地方: 网吧");
int age = 18;
if (age >= 18){
System.out.println("进入网吧,开始high!");
System.out.println("遇到了一群猪队友,开始骂街。");
System.out.println("感觉不爽,结账走人。");
}
System.out.println("回家吃饭");
}
}
1_1_9_03_选择结构_标准if-else语句
public class Demo03IfElse {
public static void main(String[] args){
int num = 13;
if (num % 2 == 0){ // 如果除以2能够余数为0,说明是偶数
System.out.println("偶数");
} else {
System.out.println("奇数");
}
}
}
public class Demo03IfElse {
public static void main(String[] args){
int num = 666;
if (num % 2 == 0){ // 如果除以2能够余数为0,说明是偶数
System.out.println("偶数");
} else {
System.out.println("奇数");
}
}
}
1_1_9_04_选择结构_扩展if-else语句
// x和y的关系满足如下:
// 如果x >= 3,那么y = 2x + 1;
// 如果-1 < x < 3,那么y = 2x;
// 如果x <= -1,那么y = 2x -1;
public class Demo04IfElseExt {
public static void main(String[] args) {
int x = -10;
int y;
if (x >= 3) {
y = 2 * x + 1;
} else if (-1 < x && x < 3) {
y = 2 * x;
}else {
y = 2 * x -1;
}
System.out.println("结果是: " + y); // 结果是: -21
}
}
1_1_9_05_练习_用if语句实现考试程序
–BUG,120分输出不及格
–修复
public class Demo05IfElsePractise {
public static void main(String[] args) {
int score = 120;
if (score >= 90 && score <= 100) {
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("数据错误");
}
}
}
1_1_9_06_练习_用if语句替换三元运算符
// 题目: 使用三元运算符和标准的if-else语句分别实现: 取两个数字当中的最大值
public class Demo06Max {
public static void main(String[] args) {
int a = 10;
int b = 20;
// 首先使用三元运算符
int max = a > b ? a : b;
System.out.println("最大值 " + max);
}
}
// 题目: 使用三元运算符和标准的if-else语句分别实现: 取两个数字当中的最大值
public class Demo06Max {
public static void main(String[] args) {
int a = 10;
int b = 20;
// 首先使用三元运算符
//int max = a > b ? a : b;
// 使用今天的if语句
int max;
if (a > b) {
max = a;
} else {
max = b;
}
System.out.println("最大值 " + max);
}
}
1_1_9_07_选择结构_标准的switch
public class Demo07Switch {
public static void main(String[] args) {
int num = 10;
switch (num) {
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; // 最后一个break语句可以省略,但是强烈推荐不要省略
}
}
}
1_1_9_08_选择结构_穿透的switch
/*
switch语句使用的注意事项:
1. 多个case后面的数值不可以重复。
2. switch后面小括号当中只能是下列数据类型
基本数据类型: byte/short/char/int
引用数据类型: String字符串、enum枚举
3. switch语句格式可以很灵活: 前后顺序可以颠倒,而且break语句还可以省略。
"匹配哪一个case就从哪一个位置向下执行,直到遇到了break或者整体结束为止。"
面试题:
11.switch是否能作用在byte 是否能作用在long 上是否能作用在String上?
答:
在Java 5以前,switch(expr)中,expr只能是byte、short、char、int。
从Java 5开始,Java中引入了枚举类型,expr也可以是enum类型,
从Java 7开始,expr还可以是字符串(String),但是长整型(long)在目前所有的版本中都是不可以的。
*/
public class Demo08SwitchNotice {
public static void main(String[] args) {
int num = 5;
switch (num) {
case 1:
System.out.println("你好");
break;
case 2:
System.out.println("我好");
break;
case 3:
System.out.println("大家好");
break;
default:
System.out.println("你好,我也好。");
break;
} // switch
}
}