import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.stream.DoubleStream;
/**
* @ClassName: HongBaoDemo
* @Description: 发红包Demo
* @Author: Wanglt
* @CreateDate: 2020年3月1日
*
*/
public class HongBaoDemo {
private double money;
private int peopleSum;
private double[] res;
public HongBaoDemo(double money, int peopleSum) {
super();
this.money = money;
this.peopleSum = peopleSum;
this.res = new double[peopleSum];
}
public double[] getRes() {
return res;
}
public void fabao() {
int temp = (int) (money * 100 - peopleSum);
int[] nums = new int[peopleSum];
Random random = new Random();
for (int i = 1; i < peopleSum; i++) {
if (temp == 0) {
break;
}
int num = random.nextInt(temp);
nums[i] = num + 1;
temp = temp - num;
}
nums[0] = temp + 1;
for (int i = 0; i < nums.length; i++) {
res[i] = nums[i] / 100.0;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入金额");
double money = scanner.nextDouble();
System.out.println("请输入人数");
int peopleSum = scanner.nextInt();
HongBaoDemo hongBao = new HongBaoDemo(money, peopleSum);
hongBao.fabao();
DoubleStream doubleStream = Arrays.stream(hongBao.getRes());
doubleStream.forEach(System.out::println);
}
}
发红包模拟
最新推荐文章于 2023-06-28 21:07:34 发布