文章分为原始代码和改进代码,但不知是否可以如此,若有大佬看到请加以指正!在此谢过(✿◕‿◕✿)
改进代码中在输入时加入判断

Admin_Log

需求:
– 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份、头等舱或经济舱。
– 按照如下功夫i则计算机票价格:
– 旺季( 5 ~ 10 月份 )头等舱9折,经济舱8.5折,淡季( 11月 ~ 来年 4 月)头等舱7折,经济舱6.5折

Admin_Log

复制代码
  1. package Test_FG;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Fly2 {
  6.  
  7.  
  8. public static void main(String[] args) {
  9. /*
  10. 需求:
  11. - 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份、头等舱或经济舱。
  12. - 按照如下功夫i则计算机票价格:
  13. - 旺季( 5 ~ 10 月份 )头等舱9折,经济舱8.5折,淡季( 11月 ~ 来年 4 月)头等舱7折,经济舱6.5折
  14. */
  15. // 分析:
  16. // - 1. 键盘录入机票原价、月份、头等舱或经济舱
  17. Scanner sc = new Scanner(System.in);
  18. System.out.println("请输入机票的价格:");
  19. int ticket = sc.nextInt();
  20. System.out.println("请输入当前月份:");
  21. int month = sc.nextInt();
  22. System.out.println("请输入舱位:\n0 头等舱\n1 经济舱");
  23. int seat = sc.nextInt();
  24.  
  25. // - 2. 先判断月份是旺季还是淡季
  26. if (month >= 5 && month <= 10) {
  27. // 旺季:
  28. // 继续判断当前机票是经济舱还是头等舱
  29. // - 3. 继续判断当前机票是经济舱还是头等舱
  30. if (seat == 0) {
  31. //若使用ticket *= 0.9 会自动强转
  32. // 头等舱
  33. ticket = (int) (ticket * 0.9);
  34. } else if (seat == 1) {
  35. // 经济舱
  36. ticket *= 0.85;
  37. } else {
  38. System.out.println("没有这个舱位");
  39. }
  40. } else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) {
  41. // 淡季:
  42. if (seat == 0) {
  43. // 头等舱
  44. ticket = (int) (ticket * 0.7);
  45. } else if (seat == 1) {
  46. // 经济舱
  47. ticket = (int) (ticket * 0.65);
  48. } else {
  49. System.out.println("没有这个舱位");
  50. }
  51. } else {
  52. // 键盘录入的月份不合法
  53. System.out.println("录入的月份不合法(没有这个月份,请输入阿拉伯数字1~12)");
  54. }
  55.  
  56. // - 4. 根据实际情况计算出对应的价格
  57. System.out.println("机票价格为:"+ ticket);
  58. }
  59. }

复制代码
  1. package Test_FG;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Fly {
  6. public static void main(String[] args) {
  7. /*
  8. 需求:
  9. - 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份、头等舱或经济舱。
  10. - 按照如下功夫i则计算机票价格:
  11. - 旺季( 5 ~ 10 月份 )头等舱9折,经济舱8.5折,淡季( 11月 ~ 来年 4 月)头等舱7折,经济舱6.5折
  12. */
  13. // 分析:
  14. // - 1. 键盘录入机票原价、月份、头等舱或经济舱
  15. Scanner sc = new Scanner(System.in);
  16. System.out.println("请输入机票的价格:");
  17. int ticket = sc.nextInt();
  18.  
  19. int month;
  20. for (; ; ) {
  21. System.out.println("请输入当前月份:");
  22. month = sc.nextInt();
  23. if (month > 12 || month < 1) {
  24. System.out.println("没有" + month + "月份,请重新输入");
  25. } else {
  26. break;
  27. }
  28. }
  29.  
  30. int seat;
  31. for (; ; ) {
  32. System.out.println("请输入当前购买的舱位:\n0 头等舱\n1 经济舱");
  33. seat = sc.nextInt();
  34. if (seat > 1 || seat < 0) {
  35. System.out.println("没有" + seat + "舱位,请重新输入");
  36. } else {
  37. break;
  38. }
  39. }
  40. // - 2. 先判断月份是旺季还是淡季
  41. // 旺季
  42. if (month >= 5 && month <= 10) {
  43. // 继续判断是头等舱还是经济舱
  44. // 头等舱
  45. if (seat == 0) {
  46. ticket *= 0.9;
  47. // 经济舱
  48. } else if (seat == 1) {
  49. ticket *= 0.85;
  50. }
  51. // 淡季
  52. }else if ((month>=1&&month<=4)||(month>=11&&month<=12)){
  53. // 头等舱
  54. if (seat == 0) {
  55. ticket *= 0.7;
  56. // 经济舱
  57. } else if (seat == 1) {
  58. ticket *= 0.65;
  59. }
  60. }
  61. System.out.println(ticket);
  62. // - 3. 继续判断当前机票是经济舱还是头等舱
  63. // - 4. 根据实际情况计算出对应的价格
  64. }
  65. }

复制代码
  1. package Test_FG;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Fly3 {
  6. public static void main(String[] args) {
  7. /*
  8. 需求:
  9. - 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份、头等舱或经济舱。
  10. - 按照如下功夫i则计算机票价格:
  11. - 旺季( 5 ~ 10 月份 )头等舱9折,经济舱8.5折,淡季( 11月 ~ 来年 4 月)头等舱7折,经济舱6.5折
  12. */
  13. // 分析:
  14. // - 1. 键盘录入机票原价、月份、头等舱或经济舱
  15. Scanner sc = new Scanner(System.in);
  16. System.out.println("请输入机票的价格:");
  17. int ticket = sc.nextInt();
  18.  
  19. int month;
  20. for (; ; ) {
  21. System.out.println("请输入当前月份:");
  22. month = sc.nextInt();
  23. if (month > 12 || month < 1) {
  24. System.out.println("没有" + month + "月份,请重新输入");
  25. } else {
  26. break;
  27. }
  28. }
  29.  
  30. int seat;
  31. for (; ; ) {
  32. System.out.println("请输入当前购买的舱位:\n0 头等舱\n1 经济舱");
  33. seat = sc.nextInt();
  34. if (seat > 1 || seat < 0) {
  35. System.out.println("没有" + seat + "舱位,请重新输入");
  36. } else {
  37. break;
  38. }
  39. }
  40. // - 2. 先判断月份是旺季还是淡季
  41. // 旺季
  42. if (month >= 5 && month <= 10) {
  43. ticket = getPrice(ticket,seat,0.9,0.85);
  44. // 淡季
  45. }else if ((month>=1&&month<=4)||(month>=11&&month<=12)){
  46. ticket = getPrice(ticket,seat,0.7,0.65);
  47. }
  48. System.out.println(ticket);
  49. // - 3. 继续判断当前机票是经济舱还是头等舱
  50. // - 4. 根据实际情况计算出对应的价格
  51. }
  52. public static int getPrice(int ticket, int seat, double First, double economy){
  53. // 头等舱
  54. if (seat == 0) {
  55. ticket *= First;
  56. // 经济舱
  57. } else if (seat == 1) {
  58. ticket *= economy;
  59. }
  60. return ticket;
  61. }
  62. }