zoukankan      html  css  js  c++  java
  • 如何生成一副Poker

    import java.util.LinkedList;
    import java.util.Random;
    //扑克类
    class Poker{
        String color;//花色
        String num;//点数
        public Poker (String color,String num) {
            this.color=color;
            this.num =num;
        }
        public String toString() {
            return "{"+color+num+"}";
        }
    }

    public class Demo{
        public static void main(String[] args) {
            LinkedList<Poker> pokers=createPoker();
            showPoker(pokers);
            System.out.println("洗牌,洗牌。。。");
            sufflePoker(pokers);
            showPoker(pokers);
        }
        //生成一副扑克
        public static LinkedList<Poker> createPoker() {
            LinkedList<Poker> list=new LinkedList<Poker>();
            String []color={"黑桃","红桃","梅花","方块"};
            String []num={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
            for (int i = 0; i < color.length; i++) {
                for (int j = 0; j < num.length; j++) {
                    list.add(new Poker(color[i], num[j]));
                }
            }
            return list;
        }
        //更好展示扑克
        public static void showPoker(LinkedList<Poker> pokers){
            for (int i = 0; i < pokers.size(); i++) {
                System.out.print(pokers.get(i));
                if (i%13==12) {
                    System.out.println();//换行
                }   
            }
        }
        //洗扑克功能
        public static void sufflePoker(LinkedList<Poker> pokers) {
            Random random=new Random();//创建随机数对象
            for (int i = 0; i < 100; i++) {//实现多次洗牌
                //随机产生两个索引值
                int index1=random.nextInt(pokers.size());
                int index2=random.nextInt(pokers.size());
                //根据索引值取出两张牌,然后交换两张牌的顺序
                Poker poker1=pokers.get(index1);
                Poker poker2=pokers.get(index2);
                pokers.set(index1, poker2);
                pokers.set(index2, poker1);
            }   
        }
    }

  • 相关阅读:
    loadrunner获取Http信息头中指定值作为参数
    soapUI使用-DataSource获取oracle库中的参数
    [转]vim编辑器---批量注释与反注释
    String() 函数把对象的值转换为字符串。
    自定义滚动条mCustomScrollbar
    css实现强制不换行/自动换行/强制换行
    在网页中添加新浪微博“加关注”按钮
    移动前端调试方案(Android + Chrome 实现远程调试)
    font-family
    移动端touch事件滚动
  • 原文地址:https://www.cnblogs.com/lifi/p/5225862.html
Copyright © 2011-2022 走看看