题目:
机票价格按照淡季旺季,头等舱和经济舱收费,输入机票原价,月份和头等舱或经济舱
按照如下规则计算飞机票价格:旺季(5-10月),头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。
代码:
public class yunsuan {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票原价:");
double price = sc.nextDouble();
System.out.println("请输入飞机票月份:");
int month = sc.nextInt();
System.out.println("请输入飞机票舱位(头等舱为1,经济舱为2):");
int seat = sc.nextInt();
double money = calc(price, month, seat);
System.out.println("机票折后价为" + money);
}
public static double calc(double price, int month, int seat) {
if(month <=0 ||month >=13){
return -1;
} else if (month >= 5 && month <= 10) {
if (seat == 1) {
price = price * 0.9;
} else {
price = price * 0.85;
}
} else if (month <= 5 || month >= 10 ) {
if (seat == 1) {
price = price * 0.7;
} else {
price = price * 0.69;
}
}
return price;
}
}
错误总结:
方法最后要有一个返回值,在整个方法的最后