Java 面向对象
判断、循环 的练习
public static void main(String[] args) {
int jipiao = 5000;
int yuefen;
// 月份选择
System.out.println("请输入您的出行月份:1~12");
Scanner input = new Scanner(System.in);
yuefen = input.nextInt();
// 月份判断
if (yuefen < 1 | yuefen > 12) {
do {
yuefen = input.nextInt();
System.out.println("请重新输入月份:");
} while (yuefen < 1 | yuefen > 12);
}
// 舱位选择
int cang;
System.out.println("请输入您选择的是头等舱还是经济舱?头等舱输1,经济舱输2");
Scanner input1 = new Scanner(System.in);
cang = input.nextInt();
// 舱位判断
if (cang == 1 & cang == 2) {
do {
cang = input.nextInt();
System.out.println("请您重选择舱位:");
} while (cang == 1 & cang == 2);
}
// 机票价格计算
// 旺季
if (yuefen >= 4 & yuefen <= 10) {
// 头等舱
if (cang == 1) {
double jiage = jipiao * 0.9;
System.out.println("您的机票价格为:" + jiage);
}
// 经济舱
else if (cang == 2) {
double jiage = jipiao * 0.6;
System.out.println("您的机票价格为:" + jiage);
} else if (cang < 1 | cang > 2) {
System.out.println("请输入正确数字进行舱的选择");
}
} else {
// 淡季
if (yuefen < 4 | yuefen > 10) {
// 头等舱
if (cang == 1) {
double jiage = jipiao * 0.5;
System.out.println("您的机票价格为:" + jiage);
}
// 经济舱
else if (cang == 2) {
double jiage = jipiao * 0.4;
System.out.println("您的机票价格为:" + jiage);
} else if (cang < 1 | cang > 2) {
System.out.println("请输入正确数字进行舱的选择");
}
}
}
}