一抽奖小程序,需十位学生信息,抽出一等奖1名,二等奖2名,三等奖3名
发布网友
发布时间:2022-04-25 19:14
我来回答
共1个回答
热心网友
时间:2023-10-13 23:39
public static void main(String[] args) {
int input = 10;
Scanner s = null;
List<Student> ls = new ArrayList<>();
ls.add(new Student("张3", '男'));
ls.add(new Student("张4", '女'));
ls.add(new Student("张5", '男'));
ls.add(new Student("张6", '男'));
ls.add(new Student("张7", '女'));
ls.add(new Student("张8", '男'));
List<Student> lscopy = new ArrayList<>();
// 由于不能重复中奖,所以抽一次就要把中奖的学生移除,所以不能在原list中操作。
lscopy.addAll(ls);
while (input != 0) {
System.out.println("选择:");
System.out.println("1:一等奖");
System.out.println("2:二等奖");
System.out.println("3:三等奖");
System.out.println("0:结束");
s = new Scanner(System.in);
input = s.nextInt();
switch (input) {
case 1:
// 随机出0-9 十个数字
int first = (int) (Math.random() * (lscopy.size() - 1));
System.out.println("一等奖:" + lscopy.get(first));
// 已经中奖的移除
lscopy.remove(first);
break;
case 2:
for (int i = 0; i < 2; i++) {
int second = (int) (Math.random() * (lscopy.size() - 1));
System.out.println("二等奖:" + lscopy.get(second));
lscopy.remove(second);
}
break;
case 3:
for (int i = 0; i < 3; i++) {
int third = (int) (Math.random() * (lscopy.size() - 1));
System.out.println("三等奖:" + lscopy.get(third));
lscopy.remove(third);
}
break;
}
}
}
自己看着改一下,没运行过不知道有没有错,大概就是这么个意思。你自己再写个学生类