• Java实例之实现淡旺季飞机票打折

    某航空公司为吸引更多的顾客推出了优惠活动。原来的飞机票价为 3000 元,活动时,4~11 月旺季,头等舱 9 折,经济舱 8 折;1~3 月、12 月淡季,头等舱 5 折,经济舱 4 折,求机票的价格。

    使用if-else语句实现淡旺季飞机票打折

    下面使用 Java 的嵌套 if 语句根据淡旺季飞机票打折求出飞机票的价格,编写 Java 程序实现代码如下:

    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);
        }
    }

    上面代码将用户输入的月份保存到 month 变量,将机票种类保存到 kind 变量。接下来判断变量 month 和 kind 的范围。如果变量 month 在 4~11,kind 为 1 则执行 result=result*0.9,为 2 则执行 result=result*0.8;变量 month 在 1~3、12,kind 为 1 则执行 result=result*0.5,为 2 则执行 result=result*0.4。当用户输入有误时,根据错误情况给予不同的提示。

    旺季经济舱出行的输出结果如下所示:

    请输入出行的月份:
    6
    选择头等舱还是经济舱?数字1为头等舱,数字2为经济舱
    2
    您选择的机票价格为:48000.0

    淡季头等舱的输出结果如下所示:

    请输入出行的月份:
    2
    选择头等舱还是经济舱?数字1为头等舱,数字2为经济舱
    1
    您选择的机票价格为:30000.0

    使用switch语句实现淡旺季飞机票打折

    上面是用嵌套 if 实现的淡旺季飞机票打折,下面我们用 switch 实现,代码如下所示:

    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);
    }

    运行结果如下所示:

    请输入出行的月份:
    6
    选择头等舱还是经济舱?数字1为头等舱,数字2为经济舱
    2
    您选择的机票价格为:48000.0

    淡季头等舱的输出结果如下所示:

    请输入出行的月份:
    2
    选择头等舱还是经济舱?数字1为头等舱,数字2为经济舱
    1
    您选择的机票价格为:30000.0

更多...

加载中...