A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 伊耿 初级黑马   /  2019-9-26 13:26  /  669 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

[Java] 纯文本查看 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

public class PokerDemo {
    public static void main(String[] args) {

        HashMap<Integer,String> hm = new HashMap<>();

        ArrayList<Integer> array = new ArrayList<>();

        String[] colors = {"♠","♣","♦","♥"};
        String[]  numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

        int index = 0;
        for (String number : numbers) {
            for (String color : colors) {
                hm.put(index,color+number);
                    array.add(index);
                    index++;
            }
        }
        hm.put(index,"大王");
        array.add(index);
        index++;
        hm.put(index,"小王");
        array.add(index);

        //洗牌
        Collections.shuffle(array);

        TreeSet<Integer> s1 = new TreeSet<>();
        TreeSet<Integer> s2 = new TreeSet<>();
        TreeSet<Integer> s3 = new TreeSet<>();
        TreeSet<Integer> dp = new TreeSet<>();

        for (int i = 0; i < array.size(); i++) {
            int s = array.get(i);
            if(i >= array.size()-3){
                dp.add(s);
            }else if(i % 3 == 2 ){
                s1.add(s);
            }else if(i % 3 == 1){
                s2.add(s);
            }else if(i % 3 == 0){
                s3.add(s);
            }
        }

        lookPoker("s1",s1,hm);
        lookPoker("s2",s2,hm);
        lookPoker("s3",s3,hm);
        lookPoker("底牌",dp,hm);
    }

    private static void lookPoker(String name, TreeSet<Integer> ts,HashMap<Integer,String> hm) {
        System.out.println(name+"的牌是");
        for (Integer t : ts) {
            String poker = hm.get(t);
            System.out.print(poker);
        }
        System.out.println();
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马