步骤
- 准备牌
- 洗牌
- 发牌
- 排序
- 看牌
一.代码实现
public class DouDiZhu {
public static void main(String[] args) {
//1.准备牌
//定义一个存储54张牌的集合ArrayList,泛型使用字符串
ArrayList<String> poker=new ArrayList<>();
//定义两个集合分别存储花色和序号
String[] colors={"♠","♥","♦","♣"};
String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//将大王小王添加到集合里
poker.add("大王");
poker.add("小王");
//循环嵌套遍历两个数组,组装53张牌
//使用增强for循环
for(String number:numbers){
for (String color : colors) {
// System.out.println(color+number);
poker.add(color+number);
}
}
System.out.println(poker);
//2.洗牌
//使用集合的工具类collections中的方法
Collections.shuffle(poker);
//3.发牌
//定义4个集合存储玩家牌和底牌
ArrayList<String> player01=new ArrayList<>();
ArrayList<String> player02=new ArrayList<>();
ArrayList<String> player03=new ArrayList<>();
ArrayList<String> diPai=new ArrayList<>();
for(int i=0;i<poker.size();i++){
String p=poker.get(i);
if(i>=51){
diPai.add(p);}
else if(i%3==0){
player01.add(p);
}else if(i%3==1){
player02.add(p);
}else if(i%3==2){
player03.add(p);
}
}
//4.排序
Collection.sort(player01);
Collection.sort(player02);
Collection.sort(player03);
Collection.sort(diPai);
//5.看牌
System.out.print("张三的牌"+player01);
System.out.println();
System.out.print("李四的牌"+player02);
System.out.println();
System.out.print("王五的牌"+player03);
System.out.println();
System.out.print("底牌"+diPai);
}
}
二. 运行结果
[大王, 小王, ♠2, ♥2, ♦2, ♣2, ♠A, ♥A, ♦A, ♣A, ♠K, ♥K, ♦K, ♣K, ♠Q, ♥Q, ♦Q, ♣Q, ♠J, ♥J, ♦J, ♣J, ♠10, ♥10, ♦10, ♣10, ♠9, ♥9, ♦9, ♣9, ♠8, ♥8, ♦8, ♣8, ♠7, ♥7, ♦7, ♣7, ♠6, ♥6, ♦6, ♣6, ♠5, ♥5, ♦5, ♣5, ♠4, ♥4, ♦4, ♣4, ♠3, ♥3, ♦3, ♣3]
张三的牌[♦6, ♠7, ♥J, ♣K, ♣Q, ♦7, ♠J, ♠8, 大王, ♠2, ♥5, ♣4, ♥8, ♣7, ♠Q, ♦2, ♠6]
李四的牌[♣10, ♣5, ♦8, ♣A, ♠4, ♠10, ♣2, ♥3, ♠3, ♥7, ♦9, ♥6, ♠K, ♥4, ♦A, ♣6, ♦5]
王五的牌[♥A, ♥10, 小王, ♥Q, ♦10, ♣9, ♦J, ♦Q, ♥9, ♠A, ♣8, ♦K, ♠5, ♦3, ♦4, ♣J, ♠9]
底牌[♥2, ♣3, ♥K]
相关推荐:Aimee.洁的 《笔记本USB接口案例-接口多态的综合案例(向下转型)》and《MySQL技术数据库基础操作命令大全》
*作者:Aimee.洁
*本文版权归作者和CSDN共有,欢迎转载,未经作者同意必须保留此版声明,否则保留追究法律责任的权利。
*喜欢的点赞关注,评论区留下宝贵的意见呐❥❥~