简单demo如下:
import com.google.common.collect.Lists;
import java.util.List;
/**
* 随机抽奖程序 demo
* @author: Nemo
* @date: 2019/1/4.
*/
public class PrizeDraw {
/**
* 得到候选人
* @return
*/
private static List<String> getPeopleSet(){
List<String> peoples = Lists.newArrayList();
peoples.add("张三");
peoples.add("李四");
peoples.add("王五");
peoples.add("周六");
peoples.add("Nemo");
peoples.add("Echo");
peoples.add("乔峰");
peoples.add("韦小宝");
return peoples;
}
/**
* 得到随机次序人员列表
* @return
*/
private static String[] getRandomPeopleSet(){
//全集,所有可能中奖的人
List<String> peoples = getPeopleSet();
//开始随机抽取,把名单打乱
String results[] = new String[peoples.size()];
int size = peoples.size();
for(int i=0;i<size;i++) {
int index = (int) (Math.random() * peoples.size());
String name = peoples.get(index);
peoples.remove(name);
results[i] = name;
}
return results;
}
/**
* 随机得到一个幸运儿
* @return
*/
private static String getFirstPeople(){
return getRandomPeopleSet()[0];
}
/**
* 得到随机分组
* @return
*/
private static List<List<String>> getGroupPeoples(int groupNum){
String[] randomSet = getRandomPeopleSet();
List<List<String>> results = Lists.newArrayList();
for(int i=0;i<randomSet.length;){
List<String> groups = Lists.newArrayList();
for(int j=0;j<groupNum;j++){
if(i>=randomSet.length){
break;
}
groups.add(randomSet[i]);
i++;
}
if(groups.size()>0) {
results.add(groups);
}
}
return results;
}
/**
* 测试主函数
* @param args
*/
public static void main(String args[]){
System.out.println("恭喜【" + getFirstPeople()+"】本轮中了大奖!");
System.out.println();
System.out.println();
//分组测试,3人一组
List<List<String>> groupPeoples = getGroupPeoples(3);
for(int i=0;i<groupPeoples.size();i++){
System.out.println("开始展示第"+(i+1)+"组候选人:");
List<String> group = groupPeoples.get(i);
for (String name : group) {
System.out.print("【" + name + "】");
}
System.out.println();
}
}
}