买飞机票水题

用户购买飞机票时, 机票原价会按照淡旺季, 头等舱还是经济舱的情况进行相应的优惠

优惠方案如下:

5-10月为旺季, 头等舱9折, 经济舱8.5折

11月到来年4月为淡季, 头等舱7折, 经济舱6.5折

请开发程序计算出用户当前机票的优化价

1.首先用最笨的方法写一个,相信很多和我一样的初学者就是喜欢写这样的,没有判断,代码看起来也不是特别的美观

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入购票的月份(输入数字1-12):");
    int month = sc.nextInt();
    System.out.println("您买的是头等舱还是经济舱(1头等舱,2经济舱):");
    int seat = sc.nextInt();
    System.out.println("机票的原价是:");
    double price = sc.nextDouble();
    if (month <=10 && month >=5){
        if (seat == 1){
            price = price * 0.9;
            System.out.println("您的座位的价格是:"+ price);
        }else
            price = price * 8.5;
        System.out.println("您的座位的价格是:"+ price);
    }else if (month == 11 || month ==12 ||month <5 && month >= 1){
        if (seat == 2){
            price = price * 0.7;
            System.out.println("您的座位的价格是:"+ price);
        }else
            price = price * 0.65;
        System.out.println("您的座位的价格是:"+ price);
    }
}
}
  1. 接下来我来突破一下自己,首先加一个可以用来判断用户是否输入的数据值正确的

    public static void main(String[] args) {
//        5-10月为旺季, 头等舱9折, 经济舱8.5折
//        11月到来年4月为淡季, 头等舱7折, 经济舱6.5折
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入当前的月份:");
        int month = sc.nextInt();
        System.out.println("请问您购买的仓位(1头等舱,2经济舱):");
        int seat = sc.nextInt();
        System.out.println("请输入您购买的机票价格:");
        int price = sc.nextInt();

        if (month<=10 &&month >=5){
            price = getPrice(seat,price,0.9,0.85);
        }else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)){
            price = getPrice(seat,price,0.70,0.65);
        }else{
            System.out.println("您输入的月份有误!");
        }
        System.out.println("您的机票价格是:" + price);

    }
    public static int getPrice(int seat,int price,double v1,double v2){

        if (seat == 1){
            price = (int)(price*v1);
        }else if (seat == 2){
            price = (int)(price * v2);
        }else{
            System.out.println("您的输入有误,没有这个仓位!");
        }
        return price;
    }

本来是想写如果不正确就在哪一步停止重新输入,不过最后不是特别的会,就是按照老师的思路携程上面这样,还是可以写出上面的代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值