机票价格按照淡季旺季、头等舱和经济舱收费,输入机票原价、月份和头等舱或者经济舱,返回最终结果。机票优惠方案如下:旺季(5-10月份)头等舱9折,经济舱8.5折;淡季(11月到来年4月)头等舱7折,经济舱6.5折。
import java.util.Scanner;
public class ProgramTest {
//机票按照淡季旺季、头等舱和经济舱收费
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入机票原价:");
double price=input.nextDouble();
System.out.println("请输入月份:");
int month=input.nextInt();
System.out.println("请输入头等舱或者经济舱:");
String type= input.next();
System.out.println("最后的价格为:"+endPrice(price, month, type));
}
public static double endPrice(double price,int month,String type){
switch (month){
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
if (type.equals("头等舱")){
return 0.9*price;
}else if (type.equals("经济舱")){
return 0.85*price;
}else
return -1;
case 1:
case 2:
case 3:
case 4:
case 11:
case 12:
if (type.equals("头等舱")){
return 0.7*price;
}else if (type.equals("经济舱")){
return 0.65*price;
}else
return -1;
default:
System.out.println("输入信息有误!!");return -1;
}
}
}
执行代码,输入数据后查看最终结果: