1.需求
2.开发流程
1.让用户输入机票原价,月份,仓库类型
2.定义方法接收信息,统计优惠后返回价格
3.代码演示
import java.util.Scanner;
/**
* 项目名称:购买飞机票
* 需求:
* ①机票价格按照淡季旺季,头等舱和经济舱,输入机票价格,月份和头等舱或经济舱
* ②机票最终优惠价格的计算方案如下:
* 旺季(5-10月份)头等舱9折,经济舱8.5折,
* 淡季(11月份到来年4月)头等舱7折,经济舱6.5折
*/
public class Test1 {
public static void main(String[] args) {
//1.让用户输入机票原价,月份,仓库类型
Scanner input = new Scanner(System.in);
//输入机票价格
System.out.print("请输入机票价格:");
double price = input.nextDouble();
System.out.println();
//输入月份
System.out.print("请输入月份:");
int month = input.nextInt();
System.out.println();
//输入类型
System.out.print("请输入仓库类型:");
String type = input.next();
System.out.println(dePrice(price, month, type));
}
//2.定义方法接收信息,统计优惠后返回价格
public static Double dePrice(Double price,int month,String type){
if(month>=5&&month<=10){
//旺季
switch (type){
case "头等舱":
price*=0.9;
break;
case "经济舱":
price*=0.85;
break;
default:
System.out.println("输入仓库类型有误,请重新输入!!!");
month = -1;
price = -1.0;
}
}else if(month==11 || month == 12 || month>=1 && month<=4){
//淡季
switch (type){
case "头等舱":
price*=0.7;
break;
case "经济舱":
price*=0.65;
break;
default:
System.out.println("输入仓库类型有误,请重新输入!!!");
price = -1.0;
}
}else {
System.out.println("输入的月份错误");
price = -1.0;
}
return price;
}
}
4.总结
①遇到判断值匹配的时候选择switch case分支结构实现
②遇到判断区间范围的时候选择if分支结构实现