用户购买飞机票时, 机票原价会按照淡旺季, 头等舱还是经济舱的情况进行相应的优惠
优惠方案如下:
5-10月为旺季, 头等舱9折, 经济舱8.5折
11月到来年4月为淡季, 头等舱7折, 经济舱6.5折
请开发程序计算出用户当前机票的优化价
1.首先用最笨的方法写一个,相信很多和我一样的初学者就是喜欢写这样的,没有判断,代码看起来也不是特别的美观
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入购票的月份(输入数字1-12):");
int month = sc.nextInt();
System.out.println("您买的是头等舱还是经济舱(1头等舱,2经济舱):");
int seat = sc.nextInt();
System.out.println("机票的原价是:");
double price = sc.nextDouble();
if (month <=10 && month >=5){
if (seat == 1){
price = price * 0.9;
System.out.println("您的座位的价格是:"+ price);
}else
price = price * 8.5;
System.out.println("您的座位的价格是:"+ price);
}else if (month == 11 || month ==12 ||month <5 && month >= 1){
if (seat == 2){
price = price * 0.7;
System.out.println("您的座位的价格是:"+ price);
}else
price = price * 0.65;
System.out.println("您的座位的价格是:"+ price);
}
}
}
接下来我来突破一下自己,首先加一个可以用来判断用户是否输入的数据值正确的
public static void main(String[] args) {
// 5-10月为旺季, 头等舱9折, 经济舱8.5折
// 11月到来年4月为淡季, 头等舱7折, 经济舱6.5折
Scanner sc = new Scanner(System.in);
System.out.println("请输入当前的月份:");
int month = sc.nextInt();
System.out.println("请问您购买的仓位(1头等舱,2经济舱):");
int seat = sc.nextInt();
System.out.println("请输入您购买的机票价格:");
int price = sc.nextInt();
if (month<=10 &&month >=5){
price = getPrice(seat,price,0.9,0.85);
}else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)){
price = getPrice(seat,price,0.70,0.65);
}else{
System.out.println("您输入的月份有误!");
}
System.out.println("您的机票价格是:" + price);
}
public static int getPrice(int seat,int price,double v1,double v2){
if (seat == 1){
price = (int)(price*v1);
}else if (seat == 2){
price = (int)(price * v2);
}else{
System.out.println("您的输入有误,没有这个仓位!");
}
return price;
}
本来是想写如果不正确就在哪一步停止重新输入,不过最后不是特别的会,就是按照老师的思路携程上面这样,还是可以写出上面的代码