已知有十六支男子足球隊參加2008 北京奧運會。寫一個程式,把這16 支球隊隨機分為4 個組。 注:參賽球隊列表見附錄 注2:使用Math.random 來產生隨機數。(也可以使用其它方法) 2. 2
阿新 • • 發佈:2018-11-11
/**
* Created by whp on 2018/7/30.
*/
public class Test {
public static void main(String[] args) {
String[] str={"象牙海岸","阿根廷","澳大利亞","塞爾維亞","荷蘭","奈及利亞","日本",
"美國","中國","紐西蘭","巴西","比利時","韓國","喀麥隆","宏都拉斯","義大利"};
List list=new ArrayList();
List list1=new ArrayList();
for (int i=0;i<str.length;i++){
list.add(str[i]);
}
while(true){
list1.clear();
while (true) {
Random random = new Random();
int index = random.nextInt(list.size());
Object s = list.get(index);
list1.add(s);
list.remove(index);
if (list1.size() == 4) {
for (Object o : list1) {
System.out.print(o + " ");
}
break;
}
}
System.out.println();
if (list.size() == 0) {
break;
}
}
}
}