抢红包小游戏

 test4 t1 = new test4();
 test4 t2 = new test4();
 test4 t3 = new test4();
 test4 t4 = new test4();
 test4 t5 = new test4();
 
t1.setName("张三");
t2.setName("李四");
t3.setName("王五");
t4.setName("赵六");
t5.setName("李逵");
 t1.start();
 t2.start();
 t3.start();
 t4.start();
 t5.start();

public class test4 extends Thread {
    //金额
    static double money = 100;
    //人数
    static int count = 3;
    //最小金额
    static final double MIN = 0.01;

    @Override
    public void run() {
        synchronized (test4.class) {
            if (count == 0) {
                System.out.println(getName() + "没有抢到红包");
            } else {
                //定义中奖金额
                double price = 0;
                if (count == 1) {
                    //不再随机
                    price = money;
                } else {
                    //进行随机
                    Random r = new Random();
                    //金额是小数
                    //100-(3-1)*0.01
                    double bounds = money - (count - 1) * MIN;
                    //随机出来的红包价值
                    price = r.nextDouble(bounds);
                    //判断比最小金额
                    if (price < MIN) {
                        price = MIN;
                    }
                }
                //金额减去抽到的金额
                money=money-price;
                count--;
                System.out.println(getName()+"抢到了"+price+"元");
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值