第四章 复杂选择结构
4.1、上机练习
答:import java.util.Scanner;
public class Luckey{
public static void main(String[]args)
Scanner input=new Scanner(System.in);
System.out.println("我行我素购物管理系统》幸运抽奖");
System.out.println("请输入4为会员号:");
int num=input.nextInt();
int baiwei=num/100%10;
int random=(int)(Math.random()*10);
do{
System.out.println("随机数为:"+random);
if(baiwei==random){
System.out.println(num+"号客户是幸运客户,获精美MP4一个");
}else{
System.out.println(num+"号客户不是幸运客户");
}
}while(naiwei!=random);
}
}
4.2、多重if选择结构
答:if(条件){
代码块
}else if(条件2){
代码块
}else{
代码块
}
实例:public class ScoreAssess{
public static void main(String[]args){
int score=70;
if(score>90){
System.out.println("成绩优秀");
}else if(sore>=80){
System.out.println("成绩良好");
}else if(score>=60){
System.out.println("成绩中等");
}else{
System.out.println("差");
}
}
}
4.3、嵌套if选择结构
答:if(条件1){
if(条件2){
代码块1
}else{
代码块2
}
}else{
代码块3
}
实例:import java.util.Scanner;
public class RunningMatch{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("请输入比赛成绩:");
int score =input.nextInt();
System.out.println("请输入性别:");
String sex=input.next();
if(score<=10){
if(sex.equals("男")){
System.out.println("进入男子组决赛");
}else if(){
System.out.println("进入女子组决赛");
}
}else{
System.out.println("淘汰");
}
}
}
4.4、上机练习
答:import java.util.Scanner;
public class CalcDiscount{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("请输入是否是会员号:是y/否n");
String identity=input.next();
System.out.println("请输入购物金额:");
double money=input.nextDouble();
if(identity.equals("y")){
if(money>200){
money=money*0.75;
}else{
money=money*0.8;
}
}else{
if(money>100){
money=money*0.8;
}
}
System.out.println("实际上支付:"+money);
}
}
4.5、switch选择结构
4.5.1、为什么使用switch选择结构
4.5.2、什么是switch选择结构
答:switch(表达式){
case 常量 1:
代码块1;
break;
case 常量 2:
代码块2;
break;
.......
default:
代码块n;
break;
}
4.5.3、如何使用switch选择结构
答:public class Compete2{
public static void main(String[]args){
int mingCi=1;
switch(mingCi){
case 1:
System.out.println("参加活动");
break;
case 2:
System.out.println("奖励笔记本一台");
break;
case 3:
System.out.println("奖励一个U盘");
break;
defualt:
System.out.println("没有奖励");
}
}
}
4.5.4、上机练习
答:import java.util.Scanner;
public class Shpping{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("\t\t\t\t\t\t欢迎使用我行我素购物管理系统1.0版");
System.out.println("\t\t\t\t\t\t\t\t\t1.登录系统");
System.out.println("\t\t\t\t\t\t\t\t\t2.退出");
System.out.println("***************************************************");
System.out.println("请选择,输入数据:");
int num=input.nextInt();
switch(num){
case 1:
System.out.println("\t\t\t欢迎使用我行我素购物管理系统");
System.out.println("*******************************************");
System.out.println("\t\t\t\t\t1.客户信息管理\n");
System.out.println("\t\t\t\t\t2.购物结算\n");
System.out.println("\t\t\t\t\t3.真情回馈\n");
System.out.println("\t\t\t\t\t4.注销\n");
System.out.println("************************************");
System.out.println("请选择,请输入数字:");
break;
case 2:
System.out.println("谢谢您的使用!");
break;
defualt:
System.out.println("输入错误!");
break;
}
}
}
4.6处理系统异常
4.7、上机练习
答:import java.util.Scanner;
public class HuanGou{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("请输入消费金额:");
double num=input.nextDouble();
if(num>=50){
System.out.println("是否参加优惠换购活动:");
System.out.println("1: 满50元,加2元换购百事可乐饮料1瓶");
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("请选择:");
int chion=input.nextInt();
switch(chion){
case 1:
if (num>=50) {
System.out.println("本次消费总金额为:"+(xiaoFeiMoney+2));
System.out.println("换购成功:百事可乐饮料1瓶!");
}
break;
case 2:
if (num>=100) {
System.out.println("本次消费总金额为:"+(xiaoFeiMoney+3));
System.out.println("换购成功:500ml可乐一瓶!");
}
break;
case 3:
if (num>=100) {
System.out.println("本次消费总金额为:"+(xiaoFeiMoney+10));
System.out.println("换购成功:5公斤面粉!");
}
break;
case 4:
if (num>=200) {
System.out.println("本次消费总金额为:"+(xiaoFeiMoney+10));
System.out.println("换购成功:1个苏波尔炒菜锅!");
}
break;
case 5:
if (num>=200) {
System.out.println("本次消费总金额为:"+(xiaoFeiMoney+20));
System.out.println("换购成功:欧莱雅爽肤水一瓶!");
}
break;
case 0:
System.out.println("本次消费总金额为:"+xiaoFeiMoney);
System.out.println("不换购!!");
break;
}
}else{
System.out.println("谢谢您光临");
}
}
}
本章总结
答:在条件判断是等值判断的情况下,可以使用switch选择结构代替多重if选择结构,在使用switch选择结构时不要忘记每个case的最后写上break语句
本章作业
答:import java.util.Scanner;
public class jipiao {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入您出行的月份:1~12");
int Y = input.nextInt();
System.out.println("请问您选择头等舱还是经济舱?头等舱输入1,经济舱输入2:");
int T = input.nextInt();
double m = 5000;
switch (Y) {
case 1:
case 2:
case 3:
case 11:
case 12:
if (T == 1) {
System.out.println("您的机票价格为:" + m * 0.5);
} else if (T == 2) {
System.out.println("您的机票价格为:" + m * 0.4);
} else {
System.out.println("输入错误 !!");
}
break;
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
if (T == 1) {
System.out.println("您的机票价格为:" + m * 0.9);
} else if (T == 2) {
System.out.println("您的机票价格为:" + m * 0.8);
} else {
System.out.println("输入错误 !!");
}
break;
default:
System.out.println("输入错误 !!");
}
}
}
方法二:import java.util.Scanner;
public class jipiao{
public static void main(String[]args){
int moon;
int select;
int Ticket = 5000;
double newTicket;
Scanner input = new Scanner(System.in);
System.out.println("请输入您出行的月份:1~12");
moon = input.nextInt();
System.out.println("请问您选择头等舱还是经济舱?头等舱输入1,经济舱输入2");
select = input.nextInt();
switch (moon) { //根据月份判断打析
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
if (select == 1) { //根据舱位判断打析
newTicket = Ticket * 0.9;
} else {
newTicket = Ticket * 0.8;
}
break;
default:
if (select == 1) { //根据舱位判断打析
newTicket = Ticket * 0.5;
} else {
newTicket = Ticket * 0.4;
}
break;
}
System.out.print("您的机票价格为:" + newTicket);
}
}