写一个拼手气红包的java代码
时间: 2023-02-20 08:57:43 浏览: 81
public class RedPacket {
public static void main(String[] args) {
// 发红包的金额
double money = 100;
// 红包总数
int count = 10;
// 剩余红包数
int remainCount = count;
// 剩余金额
double remainMoney = money;
// 每个红包金额
double perMoney = 0;
for (int i = 0; i < count - 1; i++) {
// 随机计算每个红包的金额,保留两位小数
perMoney = Math.random() * (remainMoney / remainCount * 2 - 0) + 0;
perMoney = (double)Math.round(perMoney * 100) / 100;
remainCount--;
remainMoney -= perMoney;
System.out.println("第" + (i + 1) + "个红包:" + perMoney + "元");
}
// 最后一个红包
System.out.println("第" + count + "个红包:" + remainMoney + "元");
}
}
阅读全文