collection of exercises

Exercise 1: Random Roll Caller

Requirements: There are N students in the class, implement a random roll call device

public class W1随机点名器 {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "范闲", "范建", "范统", "杜子腾", "杜琦燕", "宋合泛", "侯笼藤", "朱益群", "朱穆朗玛峰", "袁明媛");
        // 思路一:
        Random r = new Random();
		System.out.println("随机点到的是");
		int n = r.nextInt(list.size());
		System.out.println(list.get(n));
        System.out.println("点名结束");
        // 思路二 打乱集合,在获取第一个
        Collections.shuffle(list);
        System.out.println(list.get(0));
		System.out.println("点名结束");
		
        input.close();
    }
}

Exercise 2: Randomness with probability

Requirements:
There are N students in the class.
It is required that when randomizing, there is a 70% probability of a boy and a 30% probability of a girl.

    public static void main(String[] args) {
    
    
        // 班级有N名学生
        ArrayList<String> boyList = new ArrayList<>();
        ArrayList<String> girlList = new ArrayList<>();
        Collections.addAll(boyList, "范闲", "范建", "范统", "杜子腾", "宋合泛", "侯笼藤", "朱益群", "朱穆朗玛峰");
        Collections.addAll(girlList, "杜琦燕", "袁明媛", "李猜", "田蜜蜜");
        ArrayList<Integer> nums = new ArrayList<>();
        Collections.addAll(nums, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);
        Collections.shuffle(nums);
        int n = nums.get(0);
        if (n == 1) {
    
    
            Collections.shuffle(boyList);
            System.out.println(boyList.get(0));
        } else {
    
    
            Collections.shuffle(girlList);
            System.out.println(girlList.get(0));
        }
    }

Exercise 3: Randomly without repetition

Requirements:
There are N students in the class, and the students who were clicked will not be called again. However, if all students in the class have taken the roll call, the second round of roll call needs to be restarted.

the first method

public class Test3 {
    
    
    public static void main(String[] args) {
    
    
       /* 班级里有5个学生
        要求:
        被点到的学生不会再被点到。
        但是如果班级中所有的学生都点完了,需要重新开启第二轮点名。*/


        //1.定义集合
        ArrayList<String> list1 = new ArrayList<>();
        //2.添加数据
        Collections.addAll(list1, "范闲", "范建", "范统", "杜子腾", "杜琦燕", "宋合泛", "侯笼藤", "朱益群", "朱穆朗玛峰", "袁明媛");
        //创建一个临时的集合,用来存已经被点到学生的名字
        ArrayList<String> list2 = new ArrayList<>();
        //外循环:表示轮数
        for (int i = 1; i <= 10; i++) {
    
    
            System.out.println("=========第" + i + "轮点名开始了======================");
            //3.获取集合的长度
            int count = list1.size();
            //4.随机点名
            Random r = new Random();
            //内循环:每一轮中随机循环抽取的过程
            for (int j = 0; j < count; j++) {
    
    
                int index = r.nextInt(list1.size());
                String name = list1.remove(index);
                list2.add(name);
                System.out.println(name);
            }
            //此时表示一轮点名结束
            //list1 空了 list2 10个学生的名字
            list1.addAll(list2);
            list2.clear();

        }
    }
}

The second method

    public static void main(String[] args) {
    
    
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "范闲", "范建", "范统");
        // 思路一:讲这个集合复制一份,然后每次删除一个
        ArrayList<String> temp = new ArrayList<>(list);
        Collections.copy(temp, list);
        Random r = new Random();
        Scanner input = new Scanner(System.in);
        boolean index = true;
        while (index) {
    
    
            if (temp.isEmpty()) {
    
    
                // 将这个list的值重新复制到temp集合中
                temp.addAll(list);
                System.out.println("copy成功");
            }
            System.out.println("随机点到的是");
            int n = r.nextInt(temp.size());
            System.out.println(temp.get(n));
            temp.remove(n);
            System.out.println("请确认是否再次点名: true / false");
            index = input.nextBoolean();
        }
        input.close();
    }

Exercise 4: Nesting of Collections

Requirements:
Define a Map collection, the key represents the province name, and the value represents the city, but there are multiple cities.
After the addition is completed, the traversal result format is as follows:
Jiangsu Province = Nanjing City, Yangzhou City, Suzhou City, Wuxi City, Changzhou City Hubei Province = Wuhan City,
Xiaogan City, Shiyan City, Yichang City, Ezhou City
Hebei Province = Shijiazhuang City, Tangshan City, Xingtai City, Baoding City, Zhangjiakou City

    public static void main(String[] args) {
    
    
        Map<String, ArrayList<String>> map = new HashMap<>();
        map.put("湖北省", new ArrayList<>(Arrays.asList("武汉市", "孝感市", "十堰市", "宜昌市", "鄂州市")));
        map.put("江苏省", new ArrayList<>(Arrays.asList("南京市", "扬州市", "苏州市", "无锡市", "常州市")));
        map.put("河北省", new ArrayList<>(Arrays.asList("石家庄市", "唐山市", "邢台市", "保定市", "张家口市")));
        map.put("河南省", new ArrayList<>(Arrays.asList("郑州市", "洛阳市", "开封市", "平顶山市", "驻马店市")));

        // 遍历map集合
        map.forEach((String t, ArrayList<String> u) -> System.out.println(t + ":" + u));
    }

Exercise 5: Dealing cards in Doudizhu

Complete the action of shuffling and dealing cards according to the rules of Landlord. Specific rules:

Use 54 cards to shuffle the order. Three players participate in the game. The three players draw cards alternately. Each person has 17 cards, and the last three are reserved as hole cards. Complete the action of shuffling and dealing cards according to the rules of Landlord.
Specific rules:

54 cards are used to shuffle the order. Three players participate in the game. The three players draw cards alternately, each player has 17 cards, and the last three are reserved as hole cards.


    public static void main(String[] args) {
    
    
        // 创建牌
        String[] color = new String[] {
    
     "红桃", "方片", "黑桃", "梅花" };
        String[] points = new String[] {
    
     "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "J", "2" };
        String[] joker = new String[] {
    
     "小王", "大王" };
        // 创建集合来存储牌,然后在打乱集合,在分发
        ArrayList<card> list = new ArrayList<>();
        for (int i = 0; i < points.length; i++) {
    
    
            for (int j = 0; j < color.length; j++) {
    
    
                list.add(new card(color[j], points[i]));
            }
        }
        list.add(new card("特殊", joker[0]));
        list.add(new card("特殊", joker[1]));
        // 打乱集合
        Collections.shuffle(list);
        // 分发数据
        ArrayList<card> player1 = new ArrayList<>();
        ArrayList<card> player2 = new ArrayList<>();
        ArrayList<card> player3 = new ArrayList<>();
        for (int i = 0; i < list.size() - 3;) {
    
    
            player1.add(list.get(i));
            i++;
            player2.add(list.get(i));
            i++;
            player3.add(list.get(i));
            i++;
        }
        System.out.println(list);
        System.out.println(player1);
        System.out.println(player2);
        System.out.println(player3);
        System.out.println("底牌");
        System.out.println(list.get(list.size() - 3));
        System.out.println(list.get(list.size() - 2));
        System.out.println(list.get(list.size() - 1));
    }

Guess you like

Origin blog.csdn.net/everything_study/article/details/132724295