【用java写的】卖飞机票

这个是练习构造方法的,因为机票打折的过程的实现的代码是有些重复的,所以我就想着,构造成方法来实现。

题目的要求是:

机票价格按照淡季和旺季,头等舱和经济舱收费,从键盘输入机票的原价和月份,以及是头等舱还是经济舱的信息。

旺季(5-10月)头等舱打九折,经济舱8.5折,淡季(11月-来年4月)头等舱7折,经济舱6.5折。

我第一次的时候不定义方法来实现的,写得有些乱,而且重复很多。

import java.util.Scanner;

public class Mailfeijipiao {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入票价:");
        double price=sc.nextInt();
        System.out.println("请输入月份:");
        int time=sc.nextInt();
        System.out.println("请输入座位等级(1为头等舱,2为经济舱):");
        int dengji= sc.nextInt();
        if(5<=time&&time<=10){
            if (dengji==1){
                double finalprice=0.9*price;
                System.out.println("最终的价格为"+finalprice);
            }
            else {
                double finalprice=0.85*price;
                System.out.println("最终的价格为"+finalprice);
            }
        }
        else if(4<=time&&time<=11){
            if (dengji==1){
                double finalprice=0.7*price;
                System.out.println("最终的价格为"+finalprice);
            }
            else {
                double finalprice=0.65*price;
                System.out.println("最终的价格为"+finalprice);
            }
        }
     else {
            System.out.println("输入的月份有误");
        }
    }
}

然后,下面这里是我尝试定义了一个方法来的。

import java.util.Scanner;

public class Maijipiao_plus {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入票价:");
        int a = sc.nextInt();
        System.out.println("请输入月份:");
        int b = sc.nextInt();
        System.out.println("请输入票价等级(1为头等舱,2为经济舱):");
        int c = sc.nextInt();
        Dazhe(a, b, c);
    }

    public static void Dazhe(int price, int month, int dengji) {
        if (5 <= month && month <= 10) {
            if (dengji == 1) {
                int finalprice = (int) (price * 0.9);
                System.out.println("最终的价格是" + finalprice);
            } else {
                int finalprice = (int) (price * 0.85);
                System.out.println("最终的价格是" + finalprice);
            }
        } else if (4 <= month && month <= 11) {
            if (dengji == 1) {
                int finalprice = (int) (price * 0.7);
                System.out.println("最终的价格是" + finalprice);
            } else {
                int finalprice = (int) (price * 0.65);
                System.out.println("最终的价格是" + finalprice);
            }
        } else {
            System.out.println("输入的月份或等级有误");
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一支猫咔qly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值