package 专题练习; import java.util.Scanner; public class sellplaneticket { //此练习题依照itheima视频所写,题目是黑马给的,代码是本人所写,不喜勿喷,此博客只是个人分享学习经验而作 /*需求: 机票价格按照淡季旺季,头等舱和经济舱收费,输入机票原价,月份和舱型 按照规则计算机票价格: 旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11-4月)头等舱7折,经济舱6.5折*/ public static void main(String[] args) { String plane_type = type_plane(); //就差个判断了 plane_judgements(plane_type); //如果是经济舱 就用经济舱总求(总求已经直接可以把值打印出来了) //同理头等舱就用头等舱总求 } //month错误终止 public static int month_po(int month){ if(month > 12){ return -1; } return 1; } //判断舱型 public static void plane_judgements(String plane_type) { if (plane_type.equals("经济舱") ) { economics(); } else if(plane_type.equals("头等舱")){ First(); }else{ System.out.println("无此舱型"); } } //录入舱型 public static String type_plane() { System.out.println("录入舱型:"); Scanner k = new Scanner(System.in); String cabin_type = k.next(); return cabin_type; } //头等舱总求 public static void First() { int month = month_in(); if(month_po(month) == -1) { System.out.println("月份错误"); return; } double oldprice = oldrice_in(); if (low_high_seasons(month).equals("旺季")) { System.out.println("头等舱旺季票价为:" + First_Class_Peak_Season(oldprice)); } else { System.out.println("头等舱淡季票价为:" + First_Class_Off_Season(oldprice)); } } //经济舱总求 public static void economics() { int month = month_in(); if(month_po(month) == -1) { System.out.println("月份错误"); return; } double oldprice = oldrice_in(); if (low_high_seasons(month).equals("旺季")) { System.out.println("经济舱旺季票价为:" + Economy_Class_Peak_Season(oldprice)); } else { System.out.println("经济舱淡季票价为:" + Economy_Class_Off_Season(oldprice)); } } //录入月份 public static int month_in() { System.out.println("录入月份:"); Scanner k = new Scanner(System.in); int month = k.nextInt(); return month; } //录入票价 public static double oldrice_in() { System.out.println("录入原票价:"); Scanner k = new Scanner(System.in); double oldprice = k.nextInt(); return oldprice; } //还要分辨是淡季还是旺季 public static String low_high_seasons(int month) { if (month >= 1 && month <= 4 || month >= 11 && month <= 12) { return "淡季"; } else if(month >= 5 && month <= 10) { return "旺季"; } else return "输入错误"; } //我们可以分别写四个方法写经济舱的旺淡季和头等舱的旺淡季票价 //经济舱旺季 public static double Economy_Class_Peak_Season(double oldprice) { return oldprice * 0.85; } //经济舱淡季 public static double Economy_Class_Off_Season(double oldprice) { return oldprice * 0.65; } //头等舱旺季 public static double First_Class_Peak_Season(double oldprice) { return oldprice * 0.9; } //头等舱淡季 public static double First_Class_Off_Season(double oldprice) { return oldprice * 0.7; } }
java专项练习(卖机票)
于 2023-09-19 15:30:51 首次发布