Day 5 switch case 结构和循环结构
5.1 switch case 结构
1. 通用结构:
switch () {
case 常量选择1:
break;
case 常量选择2:
break;
case 常量选择3:
break;
case 常量选择4:
break;
default:
break;
}
2. 执行流程:
当代码运行到switch-case结构时,首先取出switch小括号里变量之前保存的数据,然后匹配switch的大括号内的case选项,找到对应的常量选择,执行对应的处理方式,跳出switch-case结构,如果没有找到对应的常量匹配,则执行default之后的处理方式
【break】 用于结束switch-case运行
3. 注意事项:
(1) 在switch-case结构中,有且只能执行case或者default之后的语句体,如果出现两者之外的代码是无法执行的,对Java而言时无效代码(unreachable code)
(2) 如果在case或者default后的代码中缺少break,则代码会运行到下一个break或者跳出switch-case结构
(3) 在switch-case结构中,不允许出现相同的case选项,不然系统会报错,代码无法进行操作,因为在Java中所有case原则时平级的
(4) 在switch-case结构中,default语句可以省略,它不影响代码的编译,也没有任何的语法错误,但是会缺失一种异常处理方式,而且在AJCG阿里巴巴Java开发规范中,不推荐省略default
4. 总结:
(1) switch-case结构和if-else if结构类似,是可以互通的
(2) if-else if结构适用于范围类的条件判定,区间更广泛
(3) switch-case结构是针对于某个常量的选择判定,更具有针对性
import java.util.Scanner;
class Demo1 {
public static void main(String[] args) {
int choose = 0;
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到米其林12星餐厅");
System.out.println("1. 北京烤鸭");
System.out.println("2. 重庆/四川火锅");
System.out.println("3. 擀面皮");
System.out.println("4. 烤羊排");
System.out.println("5. 佛跳墙(God Use VPN)");
choose = sc.nextInt();
switch (choose) {
case 1:
System.out.println("北京烤鸭 82RMB");
break;
case 2:
System.out.println("重庆/四川火锅 188RMB");
break;
case 3:
System.out.println("擀面皮 5RMB");
break;
case 4:
System.out.println("烤羊排 98RMB");
break;
case 5:
System.out.println("佛跳墙(God Use VPN) 998RMB");
break;
default:
System.out.println("不好意思,你选择错误");
break;
}
}
}
5.2 循环结构
5.2.1 为什么要使用循环结构
代码中会出现重复执行的语句,如果不适用循环的话,将会导致
1. 可读性极差
2. 维护性极差
3. 代码过于冗余
class Demo2 {
public static void main(String[] args) {
Syetem.out.println("烤羊排");
Syetem.out.println("烤羊排");
Syetem.out.println("烤羊排");
Syetem.out.println("烤羊排");
Syetem.out.println("烤羊排");
Syetem.out.println("烤羊排");
}
}
5.2.2 while 循环
1. 通用结构:
while () {
}
2. 执行流程:
当代码运行到while循环结构时,首先判断while之后小括号的值,为true则执行循环体(循环条件变更),为false,则跳出while循环,终止循环
class Demo3 {
public static void main(String[] args) {
int i = 1;
while (i <= 7) {
System.out.println("第" + i + "天" + ": 吃饭睡觉打豆豆");
i += 1;
}
System.out.println("i = " + i);
}
}
class Demo4 {
public static void main(String[] args) {
int i = 1;
while (i <= 100) {
System.out.println("i : " + i);
i += 1;
}
}
}
class Demo5 {
public static void main(String[] args) {
int i = 1;
int sum = 0;
while (i <= 100) {
sum += i;
i += 1;
}
System.out.println("sum : " + sum);
}
}
5.2.3 do while 循环
1. 通用结构:
do {
} while ();
2. 执行流程:
当代码执行到do-while循环结构时,无论如何先运行一次do后的循环体(循环条件变更),再来执行while后的条件判断,结果为true,则继续执行循环,若为false,终止此循环
3. 注意事项:
(1) while (); 后面的英文分号必须要有,不要忘记写
(2) 写代码时,需要明确执行第一次循环的时候会不会出现隐患
class Demo6 {
public static void main(String[] args) {
int i = 1;
do {
if (i % 2 == 0) {
System.out.println("i = " + i);
}
i += 1
} while (i <= 100);
System.out.println("---------");
int j = 2;
do {
System.out.println("j = " + j);
j += 2;
} while (j <= 100);
}
}
class Demo7 {
public static void main(String[] args) {
int i = 1;
int sum = 0;
do {
sum += i;
i += 1;
} while (i <= 150);
System.out.println("sum = " + sum);
}
}
class Demo8 {
public static void main(String[] args) {
char ch = 'A';
do {
System.out.println("ch : " + ch);
ch += 1;
} while (ch <= 'Z');
}
}
class Demo9 {
public static void main(String[] args) {
char ch = 'a';
do {
System.out.println("ch : " + ch);
ch += 1;
} while (ch <= 'z');
}
}
5.3 总结
1. 循环过程的核心是循环变量,我们需要对循环变量在循环过程中数值的变化完全掌握,如果无法明确循环的次数、循环变量的值、循环过程,可以先将变量变更的过程记录,再来写代码
2. 注意无线循环问题,保证循环条件的有效性,或者在代码中要存在一个能够跳出循环结构的机制
3. 需要注意在do-while循环中,第一次循环是没有经过条件判断的
4. while和do-while是可以互换的
5. 如果在while和do-while两者之间考虑使用哪个结构的话,一般推荐使用while结构
【程序员生存法则】 已知 可控
5.4 循环和分支结构的嵌套使用
import java.util.Scanner;
class Demo10 {
public static void main(String[] args) {
int choose = 0;
int total = 0;
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到米其林12星餐厅");
System.out.println("1. 青岛啤酒 8RMB");
System.out.println("2. 野格 88RMB");
System.out.println("3. 江小白 25RMB");
System.out.println("4. 乌苏 6RMB");
System.out.println("5. 1664 18RMB");
System.out.println("6. 下单");
while (choose != 6) {
choose = sc.nextInt();
switch (choose) {
case 1:
System.out.println("1. 青岛啤酒 8RMB");
total += 8;
break;
case 2:
System.out.println("2. 野格 88RMB");
total += 88;
break;
case 3:
System.out.println("3. 江小白 25RMB");
total += 25;
break;
case 4:
System.out.println("4. 乌苏 6RMB");
total += 6;
break;
case 5:
System.out.println("5. 1664 18RMB");
total += 18;
break;
case 6:
System.out.println("6. 下单");
break;
default:
System.out.println("选择错误");
break;
}
System.out.println("当前计费 :" + total);
}
}
}
import java.util.Scanner;
class Demo11 {
public static void main(String[] args) {
int choose = 0;
int total = 0;
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到米其林12星餐厅");
System.out.println("1. 青岛啤酒 8RMB");
System.out.println("2. 野格 88RMB");
System.out.println("3. 江小白 25RMB");
System.out.println("4. 乌苏 6RMB");
System.out.println("5. 1664 18RMB");
System.out.println("6. 下单");
do {
choose = sc.nextInt();
switch (choose) {
case 1:
System.out.println("1. 青岛啤酒 8RMB");
total += 8;
break;
case 2:
System.out.println("2. 野格 88RMB");
total += 88;
break;
case 3:
System.out.println("3. 江小白 25RMB");
total += 25;
break;
case 4:
System.out.println("4. 乌苏 6RMB");
total += 6;
break;
case 5:
System.out.println("5. 1664 18RMB");
total += 18;
break;
case 6:
System.out.println("6. 下单");
break;
default:
System.out.println("选择错误");
break;
}
System.out.println("当前计费 :" + total);
} while (choose != 6);
}
}