思路
先要得到红包的总金额,和总个数,然后产生每个红包的
随机值,每个红包的最小指为一分钱。采用一个for循环每
次产生一个红包。
import java.util.Random;
import java.util.Scanner;
public class sendRedBag {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
System.out.println("请输入红包总金额");
double total = sc.nextDouble();
System.out.println("请输入红包个数");
int count = sc.nextInt();
double min = 0.01;
for (int i = 1; i < count; i++) {
double max = total - (count - i) * min;
double bound = max - min;
double safe = (double) random.nextInt((int) (bound * 100)) / 100;
double money = safe + min;
total -= money;
System.out.println("第" + i + "个红包为:" + String.format("%.2f", money) + " 元");
}
System.out.println("第" + count + "个红包为:" + String.format("%.2f", total) + " 元");
sc.close();
}
}