java微信搶紅包簡易版
阿新 • • 發佈:2019-01-10
微信搶紅包簡易版,比較簡單,大夥別見笑,拿去直接用
import java.math.BigDecimal; import java.util.Random; public class CatchMoney { public static void main(String[] args) { double total = 100;//紅包總額 int num =8;//紅包數量 double min = 0.01;//設定最低1分錢 for(int i =1;i<num;i++){ double safe_total = (total-(num-i)*min)/(num-i);//隨機安全上限 double money = new Random().nextDouble()*(safe_total-min)+min; total = total -money; //System.out.println("第"+i+"個紅包"+setScale(money)+"元,餘額"+setScale(total)+"元"); System.out.println("第"+i+"個紅包"+setScale(money)+"元"); } System.out.println("第"+num+"個紅包"+setScale(total)+"元"); } public static double setScale(double num){ double newNum = new BigDecimal(num).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); return newNum; } }