Java教程

java案例代码3-机票打折的案例

本文主要是介绍java案例代码3-机票打折的案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

具体看代码以及注释:

import java.util.Scanner;

/**机票打折的案例,根据旺季和淡季进行处理
 *
 * */

public class Jppanduan {


    public static void main(String[] args) {
        //录入购买信息,使用方法得到最终结果
        Scanner sc=new Scanner(System.in);

        System.out.println("机票原价:");
        double price=sc.nextDouble();

        System.out.println("月份:");
        int month=sc.nextInt();

        System.out.println("仓位类型(头等舱、经济舱):");
        String type=sc.next();

        double rs=calc(price,month,type);
        System.out.println("您购买的当前机票价格是:"+rs);




    }


    //定义一个方法,形参(原价,月份,头等舱经济舱) 返回值类型申明 double

    public static double calc(double money,int month,String type){
        //2.判断月份是淡季还是旺季
        if(month>=5&&month<=10){
            //旺季
            switch (type){
                case "经济舱":
                    money*=0.85;
                    break;
                case "头等舱":
                    money*=0.9;
                    break;

                default:
                    System.out.println("您输入的仓位不正确~~");
                    money=-1;  //当前无法计算价格

            }
        }else if(month==11||month==12||month>=1&&month<=4){
            switch (type){
                case "经济舱":
                    money*=0.65;
                    break;
                case "头等舱":
                    money*=0.7;
                    break;

                default:
                    System.out.println("您输入的仓位不正确~~");
                    money=-1;  //当前无法计算价格

            }



        }else {
            System.out.println("月份有问题!");
            money=-1;
        }
        return money;



    }

}

这篇关于java案例代码3-机票打折的案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!