java重洗扑克牌

  1. 运行结果如下:

  1. 代码如下:


import java.util.Random;
//java重洗扑克牌
public class Poker {
    public static void main(String[] args) {
        String[] kinds = {"黑", "红", "花", "片"};
        String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        int[] pokers = new int[52];
        Init(pokers);
        System.out.println("初始化:");
        show(pokers, kinds, nums);
        System.out.println("洗牌后:");
//        Random rand = new Random();
//        System.out.println(rand.nextInt(5));
        shuffle(pokers);
        show(pokers, kinds, nums);
    }

    public static void Init(int[] pokers) {
        for (int i = 0; i < pokers.length; i++) {
            pokers[i] = i;
        }

    }

    public static void show(int[] pokers, String[] kinds, String[] nums) {
        for (int i = 0; i < pokers.length; i++) {
            int kind = pokers[i] / 13;   //取整找出什么类型
            int num = pokers[i] % 13;    //求余判断牌号
            System.out.print(kinds[kind] + nums[num]+" ");
            if ((i + 1) % 13 == 0) {
                System.out.println();
            }
        }
    }

    public static void shuffle(int[] pokers) {
        Random rand = new Random();    //实际取值范围0~51 后者取不到
        int temp=0;
        for (int i = 0; i < pokers.length; i++) {
            int j=rand.nextInt(52);
            if(i!=j)
            {
            temp = pokers[i];
            pokers[i]=pokers[j];
            pokers[j]=temp;
        }}

    }
}

java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值