Implementation of java example air ticket discount in low and peak seasons
•
Java
Use if else statement to realize air ticket discount in low and peak seasons
public static void main(String[] args) { Scanner sc = new Scanner(system.in); System.out.println("请输入出行的月份:"); int month = sc.nextInt(); System.out.println("选择头等舱还是经济舱?数字1为头等舱,数字2为经济舱"); int kind = sc.nextInt(); double result = 60000; // 原始价格 // 旺季的票价计算 if (month <= 11 && month >= 4) { if (kind == 1) { // 旺季头等舱 result = result * 0.9; } else if (kind == 2) { // 旺季经济舱 result = result * 0.8; } else { System.out.println("选择种类有误,请重新输入!"); } } // 淡季的票价计算 else if ((month >= 1 && month <= 3) || month == 12) { if (kind == 1) { // 淡季头等舱 result = result * 0.5; } else if (kind == 2) { // 淡季经济舱 result = result * 0.4; } else { System.out.println("选择种类有误,请重新输入!"); } } else { System.out.println("日期选择有误,请重新输入!"); } System.out.println("您选择的机票价格为:" + result); } }
请输入出行的月份: 6 选择头等舱还是经济舱?数字1为头等舱,数字2为经济舱 2 您选择的机票价格为:48000.0
请输入出行的月份: 2 选择头等舱还是经济舱?数字1为头等舱,数字2为经济舱 1 您选择的机票价格为:30000.0
Using switch statement to realize air ticket discount in low and peak seasons
public static void main(String[] args) { Scanner sc = new Scanner(system.in); System.out.println("请输入出行的月份:"); int month = sc.nextInt(); System.out.println("选择头等舱还是经济舱?数字1为头等舱,数字2为经济舱"); int kind = sc.nextInt(); double result = 60000; // 原始价格 switch (month) { // 旺季的票价计算 case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: switch (kind) { case 1: // 旺季头等舱 result = result * 0.9; break; case 2: result = result * 0.8; break; default: System.out.println("选择种类有误,请重新输入!"); break; } break; case 1: case 2: case 3: case 12: switch (kind) { case 1: // 旺季头等舱 result = result * 0.5; break; case 2: result = result * 0.4; break; default: System.out.println("选择种类有误,请重新输入!"); break; } break; default: System.out.println("日期选择有误,请重新输入!"); break; } System.out.println("您选择的机票价格为:" + result); }
Please enter the month of travel: 6 choose first class or economy class? Number 1 is first class and number 2 is economy class. The ticket price you choose is 48000.0
Please enter the month of travel: 2 choose first class or economy class? Number 1 is first class and number 2 is economy class. The ticket price you choose is 30000.0
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码