网上借鉴poker创建、洗牌方法

public class Demo {
35     
36     public static void main(String[] args) {
37         //实例化集合对象
38         LinkedList pockers=createPoker();
39         flushPockes(pockers);//洗牌
40         showPoker(pockers);//显示多少张牌
41         
42     }
43     
44     //生成扑克牌的方法
45     public  static LinkedList createPoker(){
46         //该集合用于存储扑克对象
47         LinkedList list=new LinkedList();
48         //定义数组存储所有的花色和点数
49         String[] colors={"黑色","红桃","梅花","方块"};
50         String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
51         
52         //添加集合中扑克牌
53         for(int i=0;i<nums.length;i++){
54             for(int j=0;j<colors.length;j++){
55                 list.add(new Poker(colors[j],nums[i]));
56             }
57         }
58         return list;
59     }
60     
61     //洗牌的功能
62     public static void flushPockes(LinkedList pockers){
63        //创建随机对象
64         Random random=new Random();
65         for(int i=0;i<100;i++){
66             //随机产生两个索引值
67             int a=random.nextInt(pockers.size());
68             int b=random.nextInt(pockers.size());
69             //根据索引值取出两张牌
70             Poker a1= (Poker) pockers.get(a);
71             Poker b1= (Poker) pockers.get(b);
72             pockers.set(a, b1);
73             pockers.set(b, a1);
74         }
75     }

原文地址谢谢,多学习!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值