网上借鉴poker创建、洗牌方法
程序员文章站
2022-04-06 15:55:49
...
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 }
原文地址谢谢,多学习!
上一篇: python进阶练习之——完数
下一篇: python进阶练习之——斐波那契数列
推荐阅读