淡旺季机票的价格问题(switch语句与if-else语句嵌套)

项目背景
 淡旺季机票的价格,原价机票价格为5000元,
 淡季头等舱打5折,经济舱打4折 
 旺季头等舱打9折,经济舱打8折
  
 要求 编写程序实现: 输入任意的月份与舱位来计算机票的价格
 1代表头等舱,2代表经济舱 4-10月为旺季,其他月份为淡季


package
com.summer.cn; import java.util.Scanner; /** * @author Summer * 淡旺季机票的价格,原价机票价格为5000元, * 淡季头等舱打5折,经济舱打4折 * 旺季头等舱打9折,经济舱打8折 * * 要求 编写程序实现: 输入任意的月份与舱位来计算机票的价格 * 1代表头等舱,2代表经济舱 4-10月为旺季,其他月份为淡季 */ public class Test041513 { public static void main(String[] args) { //输入原票价,同时录入乘坐的舱型和月份,并获取录入的数据 double price = 5000; Scanner sc = new Scanner(System.in); System.out.println("请输入您要乘坐的舱型"); int seat = sc.nextInt(); System.out.println("请输入请要乘坐的月份"); int month = sc.nextInt(); switch(seat){ case 1 : if(month>=4&&month<=10){ System.out.println("旺季头等舱打9折:"+price*0.9); }else{ System.out.println("淡季头等舱打5折:"+price*0.5); } break; case 2: if(month>=4&&month<=10){ System.out.println("旺季经济舱打8折:"+price*0.8); }else{ System.out.println("淡季经济舱打4折:"+price*0.4); } default: System.out.println("请输入有效的数值"); } } }
原文地址:https://www.cnblogs.com/summerdata/p/10712927.html