雙色球案例
/*
- 雙色球案例
- 紅球和 藍(lán)球
- 藍(lán)球 1個(gè) 1~16 之間
- 紅球 6個(gè) 1~33 之間
/
public class DoubleColorBall {
public static void main(String[] args) {
/ - 先將 藍(lán)球 搖出來
*/
//Random類產(chǎn)生隨機(jī)數(shù)
Random rd = new Random();
int blueBall = rd.nextInt(16)+1;
/*
* 完成紅球的搖獎(jiǎng)
*
*/
//1:創(chuàng)建紅球搖獎(jiǎng)箱
ArrayList<Integer> lotteryBox = new ArrayList<Integer>();
//往搖獎(jiǎng)箱中存儲(chǔ) 33個(gè)球
// 1~33
for(int i = 1;i<=33;i++){
lotteryBox.add(i);
}
//2:要開始搖獎(jiǎng)了
//搖獎(jiǎng)之前 要完成 一個(gè)事情 創(chuàng)建一個(gè) 存儲(chǔ)搖出來的紅球的集合
ArrayList<Integer> redBox = new ArrayList<Integer>();
//3總共要 搖獎(jiǎng)6次
for(int i =0;i<6;i++){
//隨機(jī)搖獎(jiǎng)箱中索引 根據(jù)索引找到對(duì)應(yīng)的球 在搖獎(jiǎng)箱中刪除 并且存儲(chǔ)到搖獎(jiǎng)紅球集合中
int index = rd.nextInt(lotteryBox.size());
//index就是那個(gè)要被取出的紅球的索引
Integer randomBall = lotteryBox.get(index);
//將這個(gè)球從 搖獎(jiǎng)箱中刪除掉
lotteryBox.remove(randomBall);
//存儲(chǔ)到 紅球集合中
redBox.add(randomBall);
}
System.out.println("第171497期雙色球:紅球結(jié)果"+redBox);
System.out.println("藍(lán)球:"+blueBall);
}
}