【用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
    评论
好的,我可以回答这个问题。多线程程序可以通过使用 synchronized 关键字来实现线程同步,从而避免多个线程同时访问共享资源的问题。以下是一个简单的票多线程程序的示例: ``` public class TicketSeller implements Runnable { private int tickets = 100; public void run() { while (tickets > 0) { synchronized (this) { if (tickets > 0) { System.out.println(Thread.currentThread().getName() + " sells ticket " + tickets); tickets--; } } } } public static void main(String[] args) { TicketSeller seller = new TicketSeller(); Thread t1 = new Thread(seller, "Seller 1"); Thread t2 = new Thread(seller, "Seller 2"); Thread t3 = new Thread(seller, "Seller 3"); t1.start(); t2.start(); t3.start(); } } ``` 在这个程序中,我们创建了一个 TicketSeller 类,它实现了 Runnable 接口,表示它可以作为一个线程运行。在 run() 方法中,我们使用一个 while 循环来不断票,直到票完为止。在 while 循环中,我们使用 synchronized 关键字来锁定 this 对象,以确保每次只有一个线程能够访问 tickets 变量。如果 tickets 大于 0,那么当前线程就可以出一张票,并将 tickets 减 1。 在 main() 方法中,我们创建了三个线程,并将它们都传入 TicketSeller 对象中。然后,我们启动这三个线程,它们会同时运行,但由于我们使用了 synchronized 关键字,所以它们不会同时访问 tickets 变量,从而避免了多个线程同时同一张票的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一支猫咔qly

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值