菜鸟一枚 有问题还希望大佬们指出来 轻喷
package test;
import java.util.Random;
public class testHongBao {
public static void main(String []args){
RandomRedPacket randomRedPacket = new RandomRedPacket(50,2);
for (int i = 0;i < 2;i++){
System.out.println(randomRedPacket.nextMoney() + "");
}
}
public static class RandomRedPacket{
//剩余红包钱数
private double leftMoney;
//剩余红包个数
private double leftNum;
//随机数
private Random random;
//构造方法
public RandomRedPacket(double total, double num) {
this.leftMoney = total;
this.leftNum = num;
this.random = new Random();
}
public synchronized double nextMoney(){
if (leftNum == 1){
return leftMoney;
}
if (leftMoney <= 0){
System.out.println("红包抢完了");
}
double money = (double)random.nextInt(10) * leftMoney / leftNum;
money = Math.max(money,0.01);
money = Math.min(money,this.leftMoney);
if (money < 0){
money = 0;
}
this.leftMoney -= money;
this.leftNum--;
return money;
}
}
}