集合练习之模拟扑克发牌
Posted 清风追梦enjoy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合练习之模拟扑克发牌相关的知识,希望对你有一定的参考价值。
package com.sxt.other; /* * 字符串数组+集合ArrayList * 模拟扑克发牌 */ import java.util.ArrayList; import java.util.Collections; import java.util.List; public class PokerCard { public static void main(String[] args) { String[] color = {"?","?","?","?"}; String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; //集合存储每一张牌 List<String> pokerList = new ArrayList<>(); for(int i=0; i<color.length; i++){ for(int j=0; j<num.length; j++){ pokerList.add(color[i]+num[j]);//字符串 数字和花色组合 } } pokerList.add("小王"); pokerList.add("大王"); System.out.println(pokerList); System.out.println("扑克牌的张数:"+pokerList.size()); //洗牌 Collections.shuffle(pokerList); System.out.println(pokerList); //发牌 3个人每人17张牌 还有3张底牌 List<String> client1 = new ArrayList<>(); List<String> client2 = new ArrayList<>(); List<String> client3 = new ArrayList<>(); List<String> bottomCard = new ArrayList<>(); for(int i=0; i<pokerList.size(); i++){ if(i<51){ if(i%3==0){ client1.add(pokerList.get(i)); }else if(i%3==1){ client2.add(pokerList.get(i)); }else{ client3.add(pokerList.get(i)); } }else{ bottomCard.add(pokerList.get(i)); } } System.out.println("玩家1的牌:"+client1); System.out.println("玩家2的牌:"+client2); System.out.println("玩家3的牌:"+client3); System.out.println("底牌:"+bottomCard); } }
以上是关于集合练习之模拟扑克发牌的主要内容,如果未能解决你的问题,请参考以下文章
Java学习笔记34(集合框架八:综合案例:模拟斗地主的洗牌发牌)