文章分为原始代码和改进代码,但不知是否可以如此,若有大佬看到请加以指正!在此谢过(✿◕‿◕✿)
Admin_Log
改进代码中在输入时加入判断
需求:
Admin_Log
– 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份、头等舱或经济舱。
– 按照如下功夫i则计算机票价格:
– 旺季( 5 ~ 10 月份 )头等舱9折,经济舱8.5折,淡季( 11月 ~ 来年 4 月)头等舱7折,经济舱6.5折
原始代码
package Test_FG;
import java.util.Scanner;
public class Fly2 {
public static void main(String[] args) {
/*
需求:
- 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份、头等舱或经济舱。
- 按照如下功夫i则计算机票价格:
- 旺季( 5 ~ 10 月份 )头等舱9折,经济舱8.5折,淡季( 11月 ~ 来年 4 月)头等舱7折,经济舱6.5折
*/
// 分析:
// - 1. 键盘录入机票原价、月份、头等舱或经济舱
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票的价格:");
int ticket = sc.nextInt();
System.out.println("请输入当前月份:");
int month = sc.nextInt();
System.out.println("请输入舱位:\n0 头等舱\n1 经济舱");
int seat = sc.nextInt();
// - 2. 先判断月份是旺季还是淡季
if (month >= 5 && month <= 10) {
// 旺季:
// 继续判断当前机票是经济舱还是头等舱
// - 3. 继续判断当前机票是经济舱还是头等舱
if (seat == 0) {
//若使用ticket *= 0.9 会自动强转
// 头等舱
ticket = (int) (ticket * 0.9);
} else if (seat == 1) {
// 经济舱
ticket *= 0.85;
} else {
System.out.println("没有这个舱位");
}
} else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) {
// 淡季:
if (seat == 0) {
// 头等舱
ticket = (int) (ticket * 0.7);
} else if (seat == 1) {
// 经济舱
ticket = (int) (ticket * 0.65);
} else {
System.out.println("没有这个舱位");
}
} else {
// 键盘录入的月份不合法
System.out.println("录入的月份不合法(没有这个月份,请输入阿拉伯数字1~12)");
}
// - 4. 根据实际情况计算出对应的价格
System.out.println("机票价格为:"+ ticket);
}
}
改进代码
package Test_FG;
import java.util.Scanner;
public class Fly {
public static void main(String[] args) {
/*
需求:
- 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份、头等舱或经济舱。
- 按照如下功夫i则计算机票价格:
- 旺季( 5 ~ 10 月份 )头等舱9折,经济舱8.5折,淡季( 11月 ~ 来年 4 月)头等舱7折,经济舱6.5折
*/
// 分析:
// - 1. 键盘录入机票原价、月份、头等舱或经济舱
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票的价格:");
int ticket = sc.nextInt();
int month;
for (; ; ) {
System.out.println("请输入当前月份:");
month = sc.nextInt();
if (month > 12 || month < 1) {
System.out.println("没有" + month + "月份,请重新输入");
} else {
break;
}
}
int seat;
for (; ; ) {
System.out.println("请输入当前购买的舱位:\n0 头等舱\n1 经济舱");
seat = sc.nextInt();
if (seat > 1 || seat < 0) {
System.out.println("没有" + seat + "舱位,请重新输入");
} else {
break;
}
}
// - 2. 先判断月份是旺季还是淡季
// 旺季
if (month >= 5 && month <= 10) {
// 继续判断是头等舱还是经济舱
// 头等舱
if (seat == 0) {
ticket *= 0.9;
// 经济舱
} else if (seat == 1) {
ticket *= 0.85;
}
// 淡季
}else if ((month>=1&&month<=4)||(month>=11&&month<=12)){
// 头等舱
if (seat == 0) {
ticket *= 0.7;
// 经济舱
} else if (seat == 1) {
ticket *= 0.65;
}
}
System.out.println(ticket);
// - 3. 继续判断当前机票是经济舱还是头等舱
// - 4. 根据实际情况计算出对应的价格
}
}
更新改进加方法
package Test_FG;
import java.util.Scanner;
public class Fly3 {
public static void main(String[] args) {
/*
需求:
- 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份、头等舱或经济舱。
- 按照如下功夫i则计算机票价格:
- 旺季( 5 ~ 10 月份 )头等舱9折,经济舱8.5折,淡季( 11月 ~ 来年 4 月)头等舱7折,经济舱6.5折
*/
// 分析:
// - 1. 键盘录入机票原价、月份、头等舱或经济舱
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票的价格:");
int ticket = sc.nextInt();
int month;
for (; ; ) {
System.out.println("请输入当前月份:");
month = sc.nextInt();
if (month > 12 || month < 1) {
System.out.println("没有" + month + "月份,请重新输入");
} else {
break;
}
}
int seat;
for (; ; ) {
System.out.println("请输入当前购买的舱位:\n0 头等舱\n1 经济舱");
seat = sc.nextInt();
if (seat > 1 || seat < 0) {
System.out.println("没有" + seat + "舱位,请重新输入");
} else {
break;
}
}
// - 2. 先判断月份是旺季还是淡季
// 旺季
if (month >= 5 && month <= 10) {
ticket = getPrice(ticket,seat,0.9,0.85);
// 淡季
}else if ((month>=1&&month<=4)||(month>=11&&month<=12)){
ticket = getPrice(ticket,seat,0.7,0.65);
}
System.out.println(ticket);
// - 3. 继续判断当前机票是经济舱还是头等舱
// - 4. 根据实际情况计算出对应的价格
}
public static int getPrice(int ticket, int seat, double First, double economy){
// 头等舱
if (seat == 0) {
ticket *= First;
// 经济舱
} else if (seat == 1) {
ticket *= economy;
}
return ticket;
}
}



