代码如下:
class Poker { public static void main(String[] args) { /**牌**/ List<String> cards = new ArrayList<>(); /**花色**/ List<String> colors = new ArrayList<>(); List<String> nums = new ArrayList<>(); /**花色元素**/ colors.add("♥"); colors.add("♠"); colors.add("♣"); colors.add("♦"); for (int i = 2; i <= 10; i++) { nums.add(i + ""); } nums.add("A"); nums.add("J"); nums.add("Q"); nums.add("K"); /** * 生成牌 * 1.花色+数字 * 2.大小王处理 */ for (int i = 0; i < colors.size(); i++) { for (int j = 0; j < nums.size(); j++) { String card = colors.get(i) + nums.get(j); cards.add(card); } } cards.add("大王"); cards.add("小王"); /**打乱顺序**/ Collections.shuffle(cards); List<String> player1 = new ArrayList<>(); List<String> player2 = new ArrayList<>(); List<String> player3 = new ArrayList<>(); List<String> diPai = new ArrayList<>();//预留三张牌 Iterator<String> iterator = cards.iterator(); while (iterator.hasNext()) { for (int i = 0; i < 17; i++) { player1.add(iterator.next()); iterator.remove(); } for (int i = 0; i < 17; i++) { player2.add(iterator.next()); iterator.remove(); } for (int i = 0; i < 17; i++) { player3.add(iterator.next()); iterator.remove(); } for (int i = 0; i < 3; i++) { diPai.add(iterator.next()); iterator.remove(); } } System.out.println("一号玩家:"+player1); System.out.println("二号玩家:"+player2); System.out.println("三号玩家:"+player3); System.out.println("底牌"+diPai); } }