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);
            }   
        }
    }

  • 相关阅读:
    datagrid与webAPI的数据交互(ef mvc )
    ASP.Net MVC数据传递
    dagride实现模糊查询
    MVC三层架构搭建
    web实现下拉列表多选加搜索
    php入门
    mysql实现简单的增删改查,放入xmapp自带数据库中
    ghithub中PHPOffice/PHPWord的学习
    移动端一像素问题
    点击按钮返回顶部
  • 原文地址:https://www.cnblogs.com/lifi/p/5225862.html
Copyright © 2011-2022 走看看