简单抽奖系统:
用random定义1~100个随机数,随机产生一二三等奖。使用循环结构产生一个一等奖、两个二等奖、三个三等奖,获奖号码均为1~100个随机数内的任意一个,代码如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class xitong {
private ArrayList<Integer> list;
public ArrayList<Integer> addPerson() {
// TODO Auto-generated constructor stub
if(list == null){
list = new ArrayList<Integer>();
for(int i = 1;i <= 100;i++){
list.add(i);
}
}
Collections.shuffle(list);
return(list);
}
public void choujiang(){
Random r = new Random(); //定义1~100个随机数
int a = r.nextInt(100);
System.out.println("一等奖:" + list.get(a) + "号");
list.remove(a);
for(int j = 1; j <= 2;j++){
a = r.nextInt(100);
System.out.println("二等奖:" + list.get(a) + "号");
list.remove(a);
}
for(int k = 1; k <= 3;k++){
a = r.nextInt(100);
System.out.println("三等奖:" + list.get(a) + "号");
list.remove(a);
System.out.println("恭喜你中奖了!");
}
System.out.println("谢谢参与!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
xitong b = new xitong();
b.addPerson();
b.choujiang();
}
}
运行效果如下: