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;
}
}
}
自己看著改壹下,沒運行過不知道有沒有錯,大概就是這麽個意思。妳自己再寫個學生類