欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

84452抽签Java版

时间:2023-08-10

import java.util.HashMap;import java.util.Map;import java.util.Random;public class Test { public static void main(String[] args) { Map> map = new HashMap(); Map A = new HashMap<>(); Map B = new HashMap<>(); Map C = new HashMap<>(); Map D = new HashMap<>(); A.put(0,"paris"); A.put(1,"crystal"); B.put(0,"milan"); B.put(1,"porto"); C.put(0,"forest"); C.put(1,"chelsea"); D.put(0,"mancity"); D.put(1,"barcelona"); map.put(0,A); map.put(1,B); map.put(2,C); map.put(3,D); Random random = new Random();// System.out.println(random.nextInt(4));// System.out.println(random.nextInt(4));// System.out.println(random.nextInt(4));// System.out.println(random.nextInt(4));// System.out.println(random.nextInt(4)); int i = 100; int j = 100; Map m = null; int count = 0; int group = 4; //记录组信息 //js可以用下面这个判断key是否存在 //ary.hasOwnProperty(key); 或 obj.hasOwnProperty(key); while (count < 8) { i = random.nextInt(1000) % 4; // 先抽取一个小组 if(i == group) { //如果同组重新抽 continue; } if(map.containsKey(i)){ //判断一下是否已经被抽取,防止异常 m = map.get(i); if (count % 2 == 0 || count == 0){ // 判断是否需要抽球队,这个也可以不用,直接按照先抽第一再抽第二就行了 j = random.nextInt(1000) % 2; }else { if (j == 1){ j--; }else { j++; } } if(m.containsKey(j)){ count++; System.out.println("第" + count + "个队伍是:" + m.get(j)); if (count % 2 != 0) { group = i; }else { group = 4; } map.get(i).remove(j); //抽完排除掉 } } } }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。