import java.util.HashMap;
import java.util.Random;
//用map實作抽獎
public class demo3 {
public static void main(String[] args) {
int[] arr = {888, 588, 10000, 1000, 2};
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();//中獎的獎池
Random random = new Random();//随機數
int rdom, count = 1;//接收随機數和列印獎項用的計數器
while (map.size()<arr.length){//如果中獎的個數小于獎池個數就循環
rdom = random.nextInt(arr.length);//生成一個獎池個數的随機數
if (map.get(rdom)==null){//如果中獎的獎池裡沒有這個索引,就添加否則進入循環
map.put(rdom,arr[rdom]);//将索引和獎金添加進map集合
System.out.println("第" + count + "個獎項,獎金為:" +arr[rdom]);
count++;//計數器++
}
}
}
}