Java实现抢红包程序

使用Java程序模拟抢红包

这里给出的随机抢红包算法比较简单,本例子假设当前红包是52.0元,参与抢红包的人是6人!

RedEnvelope.java

public abstract class RedEnvelope {
    public double remainMoney;     //红包当前金额
    public int remainPeople;      //当前参与抢红包的人数
    public double money;         //当前用户抢到的金额
    public abstract double giveMoney();   //抽象方法,具体怎么抢红包由子类完成
}
RandomRedEnvelope.java

import java.util.Random;
public class RandomRedEnvelope extends RedEnvelope{
    double minMoney;    //可以抢到的最小金额
    int integerRemainMoney;   //红包中的钱用分表示
    int randomMoney;        //给用户抢的钱
    Random random;
    RandomRedEnvelope(double remainMoney,int remainPeople){
        random = new Random();
        minMoney = 0.01;    //minMoney的值是0.01,保证用户至少能抢到0.01元
        this.remainMoney = remainMoney;
        this.remainPeople = remainPeople;
        integerRemainMoney = (int)(remainMoney*100);  //把钱用分表示
        if(integerRemainMoney<remainPeople*(int)(minMoney*100)){
            integerRemainMoney = remainPeople*(int)(minMoney*100);
            this.remainMoney = (double)integerRemainMoney;
        }
    }
    public double giveMoney(){
        if(remainPeople<=0){
            return 0;
        }
        if(remainPeople==1){
            money = remainMoney;
            remainPeople--;
            return money;
        }
        randomMoney =random.nextInt(integerRemainMoney);
        //该金额randomMoney在[0,integerRemainMoney]区间内
        if(randomMoney<(int)(minMoney*100)){
            randomMoney = (int)(minMoney*100);    //保证用户至少能抢到1分
        }
        int leftOtherPeopleMoney = integerRemainMoney - randomMoney;
        //leftOtherPeopleMoney是当前用户留给其余人的金额(单位是分)
        int otherPeopleNeedMoney = (remainPeople-1)*(int)(minMoney*100);
        //otherPeopleNeedMoney是保证其他人能够继续抢的最少金额(单位是分)
        if(leftOtherPeopleMoney<otherPeopleNeedMoney){
            randomMoney -= (otherPeopleNeedMoney-leftOtherPeopleMoney);
        }
        integerRemainMoney = integerRemainMoney - randomMoney;
        remainMoney = (double)(integerRemainMoney/100.0);   //钱的单位转换成元
        remainPeople--;
        money = (double)(randomMoney/100.0);
        return money;  //返回用户抢到的钱(单位是元)
    }
}
Test.java

public class Test {
    public static void main(String[] args){
       RedEnvelope redEnvelope = new RandomRedEnvelope(52.00,6);
        System.out.printf("以下用循环输出%d个人抢%.2f元的随机红包:\n",redEnvelope.remainPeople,redEnvelope.remainMoney);
        showProcess(redEnvelope);
    }
    public static void showProcess(RedEnvelope redEnvelope){
        double sum = 0;
        while(redEnvelope.remainPeople>0){
            double money = redEnvelope.giveMoney();
            System.out.printf("%.2f\t",money);
            sum = sum+money;
        }
        String s = String.format("%.2f",sum);   //金额保留两位小数
        sum = Double.parseDouble(s);
        System.out.printf("\n%.2f元的红包被抢完",sum);
    }
}

 

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
抢红包时出现红包尾数是自定义的数字,可以使用Java中的Random类来实现。具体实现思路如下: 1. 首先计算出红包总金额和红包个数。 2. 随机生成一个包含n个元素的double类型的数组,其中n为红包个数。 3. 计算数组元素之和,记为sum。 4. 遍历数组,每个元素乘以(totalAmount/sum),得到每个红包的金额,记为amount。 5. 将amount保留两位小数,并把最后一位小数四舍五入为自定义的数字,记为finalAmount。 6. 将finalAmount累加到结果数组中。 7. 最后再计算一遍结果数组元素之和,确保总金额不变。 下面是具体的Java代码实现: ```java import java.text.DecimalFormat; import java.util.Arrays; import java.util.Random; public class RedPacket { private static final DecimalFormat df = new DecimalFormat("#.00"); public static double[] divideRedPacket(double totalAmount, int totalNum, int tailNum) { double[] amountArr = new double[totalNum]; double sum = 0; Random random = new Random(); for (int i = 0; i < totalNum; i++) { amountArr[i] = random.nextDouble(); sum += amountArr[i]; } for (int i = 0; i < totalNum; i++) { double amount = totalAmount * (amountArr[i] / sum); String amountStr = df.format(amount); int dotIndex = amountStr.indexOf("."); int lastDigit = Integer.parseInt(amountStr.substring(dotIndex + 1)); if (lastDigit != tailNum) { double diff = lastDigit > tailNum ? (10 - lastDigit + tailNum) * 0.01 : (tailNum - lastDigit) * 0.01; amount += diff; amountStr = df.format(amount); } amountArr[i] = Double.parseDouble(amountStr); } double sumAmount = Arrays.stream(amountArr).sum(); double diff = totalAmount - sumAmount; if (diff > 0) { amountArr[totalNum - 1] += diff; } return amountArr; } public static void main(String[] args) { double[] amountArr = divideRedPacket(100, 10, 5); System.out.println(Arrays.toString(amountArr)); } } ``` 在本例中,我们将尾数定义为5,运行程序可以得到如下输出: ``` [5.3, 6.52, 6.02, 6.08, 6.09, 6.07, 6.16, 6.2, 6.31, 6.25] ``` 可以看到,红包的尾数都是5。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值