作为一个狂热的足球迷,在这个马上要进行欧冠抽签的日子,博主的心情是既紧张又兴奋。不知道自己喜欢的球队会抽到哪个对手,于是用JAVA,写了个小程序来模拟欧冠抽签的过程。仅供大家娱乐娱乐!
/* * Created by Travis Zeng on 2016/12/12.
*/
public class count {
public static int times = 0;
static Map<String, Integer> result = new HashMap<String, Integer>();
public static void main(String args[]) {
java.util.List<team> teamList1 = new ArrayList<>();
java.util.List<team> teamList2 = new ArrayList<>();
java.util.List<team> teamList1Copy = new ArrayList<>();
java.util.List<team> teamList2Copy = new ArrayList<>();
List<team> possibleDraw = new ArrayList<>();
teamList1.add(new team("Arsenal", "A", "England"));
teamList2.add(new team("PSG", "A", "France"));
teamList1.add(new team("Napoli", "B", "Italy"));
teamList2.add(new team("Benfica", "B", "Portugal"));
teamList1.add(new team("Barcelona", "C", "Spain"));
teamList2.add(new team("ManCity", "C", "England"));
teamList1.add(new team("ATM", "D", "Spain"));
teamList2.add(new team("Bayern", "D", "Germany"));
teamList1.add(new team("Monaco", "E", "France"));
teamList2.add(new team("Leverkusen", "E", "Germany"));
teamList1.add(new team("Dortmund", "F", "Germany"));
teamList2.add(new team("RealMadrid", "F", "Spain"));
teamList1.add(new team("Leichester", "G", "England"));
teamList2.add(new team("Porto", "G", "Portugal"));
teamList1.add(new team("Juventus", "H", "Italy"));
teamList2.add(new team("Sevilia", "H", "Spain"));
for (int i = 0; i < 1000000000; i++) {
//System.out.println();
for (team te : teamList1) {
//System.out.println(te.getName()+" "+te.getCountry()+" "+te.getGroup());
teamList1Copy.add(te);
}
for (team te : teamList2) {
teamList2Copy.add(te);
}
try {
while (teamList1Copy.size() > 0) {
Random random = new Random();
int index1 = random.nextInt(teamList1Copy.size());
team team1 = teamList1Copy.get(index1);
teamList1Copy.remove(index1);
for (team te : teamList2Copy) {
if (!(te.getGroup().equals(team1.getGroup())) && !(te.getCountry().equals(team1.getCountry()))) {
possibleDraw.add(te);
}
}
Random random1 = new Random();
int index2 = random1.nextInt(possibleDraw.size());
String draw1 = team1.getName() + " VS " + possibleDraw.get(index2).getName();
if (result.get(draw1) == null) {
result.put(draw1, 1);
} else {
int value = result.get(draw1).intValue();
value++;
result.put(draw1, new Integer(value));
}
for (team te : teamList2Copy) {
if (te.getName().equals(possibleDraw.get(index2).getName())) {
teamList2Copy.remove(te);
break;
}
}
possibleDraw = new ArrayList<team>();
}
times++;
} catch (IllegalArgumentException e) {
continue;
}
}
System.out.println();
System.out.println(times);
Object[] keys = result.keySet().toArray();
Arrays.sort(keys);
for (int i = 0; i < keys.length; i++) {
System.out.print(keys[i] + " ");
Float value = 1.0f * Integer.parseInt(result.get(keys[i]).toString()) / times;
NumberFormat nf = java.text.NumberFormat.getPercentInstance();
nf.setMaximumIntegerDigits(2);//小数点前保留几位
nf.setMinimumFractionDigits(2);// 小数点后保留几位
System.out.print(nf.format(value));
for(int k=0;k<40-(keys[i].toString().length());k++){
System.out.print(" ");
}
if(i%3==2){
System.out.println();
}
}
}
}
public class team {
private String name;
private String group;
private String country;
public team(String name,String group,String country){
this.country=country;
this.group=group;
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
最后模拟的结果: