您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用JAVA怎么實(shí)現(xiàn)一個紅包分發(fā)功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
import java.util.Arrays; import java.util.Random; import java.math.*; import java.util.Scanner; public class Main { public static long now_time; public static long seed; public static int[] get_red_packets(int money, int num) { Random random = new Random(seed); seed = random.nextLong(); int[] res = new int[num]; double[] temp=new double[num]; double sum = 0; int sum2 = 0; for (int i = 0; i < num; i++) { temp[i] = random.nextDouble(); sum += temp[i]; } for (int i = 0; i < num; i++) { res[i] = 1+ (int)(temp[i] / sum * (money-num)); sum2 += res[i]-1; } res[random.nextInt(num)]+=money-sum2-num; return res; } public static void show(int[] red_packet){ System.out.println("紅包 : " + Arrays.toString(red_packet)); } public static void main(String[] args) { int num, money; Scanner scanner = new Scanner(System.in); now_time = System.currentTimeMillis(); Random init_random = new Random(now_time); seed = init_random.nextLong(); System.out.println("請輸入要分發(fā)的紅包數(shù)量:"); num = scanner.nextInt(); System.out.println("請輸入要分發(fā)的紅包總金額(分):"); money = scanner.nextInt(); int a[] = get_red_packets(money,num); show(a); } }
上述內(nèi)容就是使用JAVA怎么實(shí)現(xiàn)一個紅包分發(fā)功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。