JAVA中用集合编写斗地主程序

本文通过Java集合实现斗地主程序,包括使用List存储54张牌的索引,进行洗牌、分发牌,以及利用Map存储牌面信息。详细介绍了每个步骤,强调了对List和Map集合的理解和运用。
摘要由CSDN通过智能技术生成

1.先直接上结果图,后边再附上代码逐步来解释
在这里插入图片描述
2.完整代码如下:

import java.util.*;

public class Doudizhu {
    public static void main(String[] args) {
        //准备牌
        //添加牌索引
        List<Integer> index = new ArrayList<>();
        //Collections.addAll(index,1,2,3);
        for (int i = 0; i < 54; i++) {
            index.add(i);
        }
        //System.out.println(index);

        //定义数组存储花色和13张牌,添加到一个List集合
        List<String> array = new ArrayList<>();
        array.add("大王");
        array.add("小王");
        String[] colors = {"♠", "♥", "♣", "♦"};
        String[] nums = {"2","A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        for (String num : nums){
            for (String color : colors){
                array.add(color + num);
            }
        }
        //  System.out.println(array);

        //存到Map集合
        Map<Integer, String> poker = new HashMap<>();
        int i = 0;
        for (Integer integer : index) {
            poker.put(integer,
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值