项目案例:
某航空公司为吸引更多的顾客推出了优惠活动。
原来的飞机票价为 60000 元,活动时,
4~11 月旺季,头等舱 9 折,经济舱 8 折;
1~3 月、12 月淡季,头等舱 5 折,经济舱 4 折,
求机票的价格。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// 输入出行月份
System.out.print("请选择出行月份:");
int month = new Scanner(System.in).nextInt();
// 输入乘坐级别
System.out.print("您准备乘坐的级别:头等舱(输入1)||经济舱(输入2):");
int sc = new Scanner(System.in).nextInt();
// 机票原价60000
double ticket = 60000;
// 外层if判断月份,内层if判断级别,sc==1代表头等舱,sc==2代表经济舱
if (month >= 4 && month <= 11) {
if (sc == 1)
ticket = 0.9 * ticket;
else if (sc == 2)
ticket = 0.8 * ticket;
else
System.out.println("级别输入错误");
} else if ((month > 0 && month < 4) || month == 12) {
if (sc == 1)
ticket = 0.5 * ticket;
else if (sc == 2)
ticket = 0.4 * ticket;
else
System.out.println("级别输入错误");
} else if ((month <= 0 || month > 13) && (sc != 1 && sc != 2)) {
System.out.println("月份输入错误");
System.out.println("级别输入错误");
} else if ((month <= 0 || month > 13) && (sc == 1 || sc == 2)) {
System.out.println("月份输入错误");
}
// 输出最终价格
if ((month > 0 && month < 13) && (sc == 1 | sc == 2))
System.out.println("您的票价为:" + ticket);
}
}
出错有三种:1.月份出错
2.级别出错
3.两个都出错