# 说明
某航空公司为吸引更多的顾客推出了优惠活动。原来的飞机票价为 60000 元,活动时,4~11 月旺季,头等舱 9 折,经济舱 8 折;1~3 月、12 月淡季,头等舱 5 折,经济舱 4 折,求机票的价格。
实现:
import java.util.Scanner; | |
class start { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("---------------欢迎选择xx航空---------------"); | |
double Price = 60000.00; | |
Double coefficient_1; | |
Double coefficient_2; | |
int month = 0, choice = 0; | |
System.out.println("输入q或者Q进入系统,输入其他则不进入"); | |
String in = scanner.nextLine(); | |
if (in.equals("q") || in.equals("Q")) { | |
while (true) { | |
System.out.println("请输入您要出行的月份"); | |
if (scanner.hasNextInt()) { | |
month = scanner.nextInt(); | |
} else { | |
System.out.println("请输入纯数字!!"); | |
// 把上一条的不规范数据删去(相当于指针下移) | |
scanner.next(); | |
continue; | |
} | |
if (month > 12 || month < 1) { | |
System.out.println("您输入的月份有误!请重新输入"); | |
continue; | |
} else if (month < 4 || month == 12) { | |
coefficient_1 = 0.5; | |
coefficient_2 = 0.4; | |
} else { | |
coefficient_1 = 0.9; | |
coefficient_2 = 0.8; | |
} | |
System.out.println("请输入选择:1 为头等舱 2 为经济舱 3 重新选择月份"); | |
if (scanner.hasNextInt()) { | |
choice = scanner.nextInt(); | |
} else { | |
System.out.println("请输入纯数字!!"); | |
// 把上一条的不规范数据删去(相当于指针下移) | |
scanner.next(); | |
continue; | |
} | |
while (choice != 1 && choice != 2 && choice != 3) { | |
System.out.println("输入有误!,请重新输入选择:"); | |
choice = scanner.nextInt(); | |
} | |
if (choice == 1) { | |
System.out.println("您需要支付:" + Price * coefficient_1 + "元"); | |
break; | |
} else if (choice == 2) { | |
System.out.println("您需要支付:" + Price * coefficient_2 + "元"); | |
break; | |
} else { | |
continue; | |
} | |
} | |
} | |
scanner.close(); | |
} | |
} |