扑克牌-做牌、洗牌

创建初始化动态数组

public class ArrayTest3 {
    public static void main(String[] args) {
        //目标:完成斗地主的做牌,洗牌
        start();
    }
    public static void start() {
        //创建一个动态化初始数组
        String [] poker = new String[54];

花色、数字排列组合

String [] colors = {"♠","♥","♣","♦"};
String [] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

int index = 0;
        for (int i = 0; i < colors.length; i++) {
            for (int j = 0; j < numbers.length; j++) {
                poker[index] = colors[i] + numbers[j];
                index++;
            }
        }
        poker[index++] = "小王";
        poker[index] = "大王";
 
        System.out.println("新牌");
            for (int i = 0; i < poker.length; i++) {
                System.out.print(poker[i] + " ");
            }
            System.out.println();

洗牌

for (int i = 0; i < poker.length; i++) {
            //获取一个索引
            int index1 = (int)(Math.random() * poker.length);
            //获取另一个索引
            int index2 = (int)(Math.random() * poker.length);

            //每一次都需要index1与index2进行数据交换
            //定义一个临时变量,存储index2的数据
            String temp = poker[index2];
            //将index1的数据,赋值给index2
            poker[index2] = poker[index1];
            //将临时变量的数据,赋值给index1
            poker[index1] = temp;
        }
        System.out.println("洗牌后");
        for (int i = 0; i < poker.length; i++) {
            System.out.print(poker[i] + " ");
        }
    }
}
posted @   菠萝屋-海绵宝宝  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示