机票价格按照淡季旺季,头等舱和经济舱收费,输入机票原价,月份和头等舱或经济舱。 机票最终优惠价格的计算方案如下:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折
public class MethodTest5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入你的原价机票:");
double jp = sc.nextInt();
System.out.printf("请输入你的舱位类型(经济舱或者头等舱):");
String cw = sc.next();
System.out.println("请输入你的购票月份:");
int moth = sc.nextInt();
double jisuan = youhui(jp, cw, moth); //使用方法计算机票优惠价格
System.out.print("机票优惠后的价格是:"+jisuan); //输出机票优惠后的价格
}
//使用方法进行机票价格优惠计算
public static double youhui(double jp, String cw, int moth) {
if (moth > 4 && moth < 11) { //月份在5-10月,旺季
switch (cw) {
case "头等舱":
jp *= 0.9; //机票=机票*0.9;
break;
case "经济舱":
jp *= 0.85;
break;
default: //不是头等舱也不是经济舱
System.out.println("输入错误,请重新输入");
return -1;
}
} else if (moth > 10 && moth < 5) { //月份在11月到来年四月,淡季
switch (cw) {
case "头等舱":
jp *= 0.7;
break;
case "经济舱":
jp *= 0.65;
break;
default: //不是头等舱也不是经济舱
System.out.println("输入错误,请重新输入");
return -1;
}
} else { //既不属于淡季也不属于旺季
System.out.printf("输入的月份错误,请重新输入");
return -1;
}
return jp;
}
}