模拟微信抢红包

package qianghongbao;

import java.util.Random;

public class HongBao {
    private double remainMoney;  //剩余的钱
    private int remainSize;      //剩余的红包数量

    public HongBao(double total, int count) {
        this.remainMoney = total;
        this.remainSize = count;
    }

    public double getRemainMoney() {
        return remainMoney;
    }
    public int getRemainSize() {
        return remainSize;
    }

    public double getRandomMoney(){

        double money;
        if(remainSize == 1){
            remainSize--;
            money = (double)Math.round(remainMoney * 100 )/100;
        }
        else if(remainSize > 1){
            double min = 0.01;
            double max = remainMoney / remainSize * 2;
            money = new Random().nextDouble() * max;
            money = money <= min ? min : money;
            money = Math.floor(money * 100) / 100;
            remainSize--;
            remainMoney -= money;
        }
        else {
            money = 0;
        }
//        System.out.println(Thread.currentThread().getName()+
//                "抢到"+money+"元,红包剩余"+remainSize+"个");
        return money;
    }
}

package qianghongbao;

public class UserThread implements Runnable{
    private HongBao hongBao;
    private double max = -1;
    String king = null;

    public UserThread(HongBao hongBao) {
        this.hongBao = hongBao;
    }

    public String getKing() {
        return king;
    }

    @Override
    public void run() {
        synchronized (this){
            double money = hongBao.getRandomMoney();
            if(money > max){
                max = money;
                king = Thread.currentThread().getName();
            }
            if(money == 0) {
                System.out.println("时刻"+System.currentTimeMillis()+","+
                        Thread.currentThread().getName() + ":你们是坐在路由器上抢的吗!");
            }
            else {
                System.out.println("时刻"+System.currentTimeMillis()+","+
                        Thread.currentThread().getName() +
                        ":抢到 " + money + "元,红包剩余"+hongBao.getRemainSize()+"个");
            }
        }
    }
}


package qianghongbao;

import java.util.Scanner;

public class QiangHongBao {
    public static void main(String[] args) throws InterruptedException{
        System.out.println("请输入红包金额红包个数");
        Scanner sc = new Scanner(System.in);
        double total = sc.nextDouble();
        int count = sc.nextInt();

        HongBao hongBao =new HongBao(total,count);
        UserThread user = new UserThread(hongBao);
        System.out.println("请输入模拟抢红包人数");
        int num = sc.nextInt();
        for(int i=0;i<num;i++) {
            new Thread(user).start();
        }
        Thread.sleep(3000);
        System.out.println("运气王:"+user.getKing());
        sc.close();
    }
}


在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值