ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

84452抽签Java版

2022-02-04 20:36:10  阅读:154  来源: 互联网

标签:count map 抽签 Java Map 84452 random nextInt put


import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Test {


    public static void main(String[] args) {
        Map<Integer, Map<Integer,String>> map = new HashMap();

        Map<Integer,String> A = new HashMap<>();
        Map<Integer,String> B = new HashMap<>();
        Map<Integer,String> C = new HashMap<>();
        Map<Integer,String> 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;   //记录组信息

        /**
         * 这个抽签是先抽小组,再抽球队(这步可以直接按名次来),同组使用group字段进行分别,只用了基础map结构,js应该可改造
         */

        //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);  //抽完排除掉
                }
            }
        }

    }
}

标签:count,map,抽签,Java,Map,84452,random,nextInt,put
来源: https://blog.csdn.net/yiyuzz/article/details/122785537

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有