Java:买飞机票

案例:买飞机票

如图

 代码演示如下:

public class Test1 {
    public static void main(String[] args) {
        //目标:完成买飞机票的价格计算
        //1.让用户输入机票原价,月份,仓位类型
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入机票的原价捏~~~");
        double money = sc.nextDouble();

        System.out.println("你要几月起飞嘞~~~");
        int month = sc.nextInt();

        System.out.println("你什么档次?坐头等舱还是经济舱?");
        String type = sc.next();

        //4.调用方法,统计结果
        System.out.println("机票优惠后的价格是" + calc(money, month, type));
    }
    //2.定义方法接收信息,通计优惠后的价格返回
    public static double calc(double mony, int month, String type){
        //3.判断用户信息的选择情况
        if (month >=5 && month <= 10){
            //旺季
            switch (type){
                case "头等舱":
                    mony *= 0.9;
                    break;
                case "经济舱":
                    mony *= 0.85;
                    break;
                default:
                    System.out.println("你输的是啥仓捏,不太对哦~");
                    mony = -1;
            }
        }else if (month == 11 || month == 12 || month >=1 && month <= 4){
            //淡季
            switch (type){
                case "头等舱":
                    mony *= 0.7;
                    break;
                case "经济舱":
                    mony *= 0.65;
                    break;
                default:
                    System.out.println("你输的是啥仓捏,不太对哦~");
                    mony = -1;
            }
        }else {
            System.out.println("你输入的月份有问题捏~~~");
            mony = -1;
        }
        return mony;
    }
}

1.遇到判断值匹配的时候选择什么结构实现?

  • 使用switch分支结构实现。

2.遇到判断区间范围的时候选择什么结构实现?

  • 使用if分支结构实现。
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以看出Java飞机机票的价格是根据淡季和旺季来确定的。具体的实现方式可能需要进一步的代码或者说明。以下是一个简单的例子来演示如何根据淡季和旺季来确定机票价格: ```java import java.util.Scanner; public class TicketPrice { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入机票原价:"); double originalPrice = scanner.nextDouble(); System.out.print("请输入月份(1-12):"); int month = scanner.nextInt(); System.out.print("请输入舱位类型(头等舱/经济舱):"); String cabinType = scanner.next(); double finalPrice = 0; if (month >= 4 && month <= 10) { // 旺季 if (cabinType.equals("头等舱")) { finalPrice = originalPrice * 1.5; } else if (cabinType.equals("经济舱")) { finalPrice = originalPrice * 1.2; } } else { // 淡季 if (cabinType.equals("头等舱")) { finalPrice = originalPrice * 1.3; } else if (cabinType.equals("经济舱")) { finalPrice = originalPrice * 1.1; } } System.out.println("机票最终价格为:" + finalPrice); } } ``` 这个例子中,用户需要输入机票的原价、月份和舱位类型,然后根据输入的信息来计算机票的最终价格。如果月份在4-10之间,则为旺季,头等舱价格为原价的1.5倍,经济舱价格为原价的1.2倍;如果月份不在4-10之间,则为淡季,头等舱价格为原价的1.3倍,经济舱价格为原价的1.1倍。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值