1.理论总结
1.switch选择结构语法执行过程:
先计算并获得switch后面小括号里面的表达式或变量的值,然后将计算结果顺序与每个case后的常量比较,
当二者相当时,执行这个case块中的代码;当遇到break时,就跳出switch选择结构,执行switch选择结构
之后的代码。
2.switch和多重if选择结构的区别与相同:
相同:它们都是用来处理多分支条件的结构;
区别:但是switch选择结构只能用于等值条件判断的情况,多重if选择结构没有switch选择结构的限制,
特别适合某个变量处于某个连续区间时的情况。
3.break:只要满足这个条件 执行完动作 就会跳出这个选择;default 相当于 else。
4.switch选择结构可以嵌套if选择结构。
2.操作题
1.上机练习1
package DoceCode;
import java.util.Scanner;
public class Doce4 {
public static void main(String[] args) {
System.out.println("\n\t\t欢迎使用我行我素购物管理系统\n");
System.out.println("\t\t\t1. 登录系统\n");
System.out.println("\t\t\t2. 退出\n");
System.out
.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.print("请选择,输入数字:");
Scanner good = new Scanner(System.in);
int nice = good.nextInt();
switch (nice) {
case 1: {
System.out.println("\n\t\t欢迎使用我行我素购物管理系统\n");
System.out
.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t\t\t\t1. 客户信息管理\n");
System.out.println("\t\t\t\t2. 购物结算\n");
System.out.println("\t\t\t\t3. 真情回馈\n");
System.out.println("\t\t\t\t4. 注销\n");
System.out
.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.print("请选择,输入数字:");
Scanner big = new Scanner(System.in);
int jkd = big.nextInt();
switch (jkd) {
case 1:
System.out.println("购物系统管理>客户信息管理");
System.out.println("1.显示所有客户信息");
System.out.println("2.添加客户信息");
System.out.println("3.修改客户信息");
System.out.println("4.查询客户信息");
break;
case 2:
System.out.println("购物系统管理>购物结算");
break;
case 3:
System.out.println("购物系统管理>真情回馈");
System.out.println("1.幸运大放送");
System.out.println("2.幸运抽奖");
System.out.println("3.生日问候");
break;
case 4:
System.out.println("购物系统管理>注销");
break;
default:
System.out.println("您的输入有误,请重新输入");
break;
}
break;
}
case 2:
System.out.println("谢谢您的使用");
break;
default:
System.out.println("您的输入有误,请重新输入");
break;
}
}
}
2.上机练习2
package DoceCode;
import java.util.Scanner;
public class Doce6 {
public static void main(String[] args) {
System.out.print("请输入消费金额:");
Scanner good = new Scanner(System.in);
if (good.hasNextInt()) {
int nice = good.nextInt();
System.out.println("是否参加优惠换购活动:");
System.out.println("1:满50元,加2元换购百事可乐饮料一瓶");
System.out.println("2:满100元,加3元换购500ml可乐一瓶");
System.out.println("3:满100元,加10元换购5公斤面粉");
System.out.println("4:满200元,加10元可换购1个苏泊尔炒菜锅");
System.out.println("5:满200元,加20可换购欧莱雅爽肤水一瓶");
System.out.println("0:不换购");
System.out.print("请选择:");
Scanner ice = new Scanner(System.in);
String name = "";
boolean pig = true;
if (ice.hasNextInt()) {
int look = ice.nextInt();
switch (look) {
case 1:
if (nice >= 50) {
nice += 2;
name = "百事可乐饮料一瓶。";
} else {
System.out.println("金额不足");
pig = false;
}
break;
case 2:
if (nice >= 100) {
nice += 3;
name = "500ml可乐一瓶。";
} else {
System.out.println("金额不足");
pig = false;
}
break;
case 3:
if (nice >= 100) {
nice += 10;
name = "5公斤面粉。";
} else {
System.out.println("金额不足");
pig = false;
}
break;
case 4:
if (nice >= 200) {
nice += 10;
name = "1个苏泊尔炒菜锅。";
} else {
System.out.println("金额不足");
pig = false;
}
break;
case 5:
if (nice >= 200) {
nice += 20;
name = "欧莱雅爽肤水一瓶。";
} else {
System.out.println("金额不足");
pig = false;
}
break;
case 0:
nice += 0;
break;
default:
System.out.println("您的输入有误,请重新输入");
break;
}
System.out.println("本次消费费总金额:" + nice);
if (look > 0 && look <= 5 && pig == true) {
System.out.println("成功换购:" + name);
} else if (look == 0) {
System.out.println("不换购");
} else {
System.out.println("换购失败");
}
} else {
System.out.println("对不起,请您输入合法的数据");
}
} else {
System.out.println("对不起,请您输入合法的数据");
}
}
}
3.
package DoceCode;
import java.util.Scanner;
public class Doce1 {
public static void main(String[] args) {
Scanner look = new Scanner(System.in);
System.out.println("请输入星期几:");
int ice = look.nextInt();
switch (ice) {
case 1:
case 3:
case 5:
System.out.println("学习编程");
break;
case 2:
case 4:
case 6:
System.out.println("学习英语");
break;
case 7:
System.out.println("休息");
break;
default:
System.out.println("输入错误");
break;
}
}
}
4.
package DoceCode;
import java.util.Scanner;
public class DoceA {
public static void main(String[] args) {
Scanner good = new Scanner(System.in);
System.out.println("请输入第一个操作数:");
if(good.hasNextInt()){
int nice = good.nextInt();
Scanner big = new Scanner(System.in);
System.out.println("请输入第二个操作数:");
if(big.hasNextInt()){
int ice = big.nextInt();
int mk = nice * ice;
int hgk = mk;
System.out.println("计算结果:"+nice+"*"+ice+ "="+hgk);
}else{
System.out.println("请输入正确的数字!");
}
}else{
System.out.println("请输入正确的数字!");
}
}
}