JAVA homework-red envelope distribution

I saw the number of visits and likes next door. I also have a blog, probably nobody reads it.
Problem analysis
1. How to ensure the random amount of money.
2. Keeping two decimal places will lead to the problem that the distributed amount does not match the total amount.
Solution
1. We use an array to store the random numbers of the red envelopes (ran array), and use a variable (sum) to accumulate each random number, so that the amount of red envelopes grabbed by the i-th person is ans[i] = ran[i]*money/sum;
2, we divide There will be a lot of decimals, and we usually grab red envelopes with only 2 decimals, so we should only keep two decimals, but suppose there are 10,000 red envelopes like 0.001, everyone grabs 0.001, we only keep two Bit means that the amount we grab is 0. Tencent made 10 yuan with tears and blood. (It doesn't seem to make much...), so we should adopt the following scheme.

 double[] ans  = new double[num];    //用来存取每个人分到的金额。
        double sum2= 0;                     //用来存取每个分到的金额的和
        for(int i=0;i<num;i++)
        {
    
    
            ans[i] = (int)(ran[i]*money/sum * 100);         //ran[i]*money/sum 即这个人应该分到的金额,注意ran[i]/sum 因为sum和ran[i]都是随机数所以可以随机分发红包。
            sum2 += ans[i];                                     //(ran[i]*money/sum * 100);  乘100即将小数点后两位变成了整数,然后转换为int即保留了两位小数。
            ans[i]/=100;
        }
        sum2/=100;                              //将sum2处于一百,即生成小数点后两位
        double temp = money - sum2;             //temp = money - sum2 temp 即因为每份红包只保留2位小数导致与红包总金额的差值
        ans[r.nextInt(num)] += temp;            //将这个差值随机加入一个红包内

Then you can type code happily.
Main function code:

package ex1;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        double money;
        int num;
        Scanner cin = new Scanner(System.in);

        do{
    
    
            System.out.print("请输入红包总金额:");
            money = cin.nextDouble();
            if(money<0.01) System.out.println("红包总金额不合法,请重新输入。");
        }while(money <0.01);

        do{
    
    
            System.out.print("请输入红包个数:");
            num = cin.nextInt();
            if(num<=0 || num>=10005)  System.out.println("红包个数不合法,请重新输入。");
            if(money/num < 0.01) System.out.println("每个人分到的钱连0.01都不到,请重新输入份数");
        }while(num<=0 ||(money/num < 0.01));

        RedPacket rp0 = new RedPacket(money,num);

        int[] ran = new int[num]; //用来存取随机数
        Random r = new Random();
        int sum = 0,x;  //sum用来记录所有随机数的和,x用来存取随机数。
        for(int i = 0;i<num;i++)
        {
    
    
            x = r.nextInt(100);
            sum += x;
            ran[i] =x;
        }

        double[] ans  = new double[num];    //用来存取每个人分到的金额。
        double sum2= 0;                     //用来存取每个分到的金额的和
        for(int i=0;i<num;i++)
        {
    
    
            ans[i] = (int)(ran[i]*money/sum * 100);         //ran[i]*money/sum 即这个人应该分到的金额,注意ran[i]/sum 因为sum和ran[i]都是随机数所以可以随机分发红包。
            sum2 += ans[i];                                     //(ran[i]*money/sum * 100);  乘100即将小数点后两位变成了整数,然后转换为int即保留了两位小数。
            ans[i]/=100;
        }
        sum2/=100;                              //将sum2处于一百,即生成小数点后两位
        double temp = money - sum2;             //temp = money - sum2 temp 即因为每份红包只保留2位小数导致与红包总金额的差值
        ans[r.nextInt(num)] += temp;            //将这个差值随机加入一个红包内
        double flag = 0;    //检测各个红包金额的和是否可以等于总金额
        for(int i = 0;i<num;i++)
        {
    
    
            flag += ans[i];
            System.out.printf("%.2f\n",ans[i]);
        }
        if(Math.abs(flag-money) < 1e-8){
    
    
            System.out.println("相等");
        }else System.out.println("不相等");
    }
}

Red envelope code:

package ex1;

public class RedPacket {
    
    
    private double money;   ///红包金额
    private int num;    //红包个数
    public RedPacket(double money, int num) {
    
    
        this.money = money;
        this.num = num;
    }

    public RedPacket(){
    
    }

    public double getMoney() {
    
    
        return money;
    }

    public int getNum() {
    
    
        return num;
    }

    public void setMoney(double money) {
    
    
        this.money = money;
    }

    public void setNum(int num) {
    
    
        this.num = num;
    }
}

The link next door
may still have uncompleted details. Welcome everyone to correct me.

Guess you like

Origin blog.csdn.net/weixin_45822897/article/details/110734534