标签:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class choujiang {
private ArrayList<Integer> list;
public void deal(){
//向list容器中添加指定数量num的整数
if (list == null){
list = new ArrayList<Integer>();
for(int i=1;i<=100;i++){
list.add(i);
}
}
//打乱list中的元素的顺序
Collections.shuffle(list);
}
//抽奖的方法:指出指定数量S的奖项
public void draw(){
Random rd = new Random();
int index1 =rd.nextInt(list.size());
list.remove(index1);
Collections.shuffle(list);
int index2 =rd.nextInt(list.size());
list.remove(index2);
Collections.shuffle(list);
int index3 =rd.nextInt(list.size());
System.out.println("获得一等奖的号码是:"+list.get(index1));
System.out.println("获得二等奖的号码是:"+list.get(index2));
System.out.println("获得三等奖的号码是:"+list.get(index3));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
choujiang cj = new choujiang();
cj.deal();
cj.draw();
}
}
标签:
原文地址:http://www.cnblogs.com/guangyeye/p/5503412.html