java用数组实现随机不重复抽奖
生活随笔
收集整理的這篇文章主要介紹了
java用数组实现随机不重复抽奖
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.Random;/*** 用獎池隨機抽獎*/
public class mode2 {public static void main(String[] args) {int[] arr = {888, 588, 10000, 1000, 2}; //獎池int[] arr1 = new int[arr.length]; //一已經中獎的獎池int intr;//用于接收隨機數int count = 0;//用于已經中獎的數組索引Random random = new Random();boolean b = true;//for循環的判斷索引while (count <= 4) {intr = random.nextInt(arr.length); //隨機生成獎池個數的隨機數for (int anArr1 : arr1) {if (arr[intr] == anArr1) {b = false;break;}}//判斷已經中獎的獎池是否有對應 的獎金,fase為沒有,進行下次抽獎if (b) {arr1[count] = arr[intr];//打印中獎順序System.out.println("第" + (count + 1) + "個獎項,獎金為:" + arr1[count]);count++;//已經中獎的數組索引后移//如果沒有對應的索引} else {b = true;}}}
}
總結
以上是生活随笔為你收集整理的java用数组实现随机不重复抽奖的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于element-ui,vue的htm
- 下一篇: ashDisp.exe进程安全吗 as