Java随机抽扑克牌

描述

一副扑克牌(除大小王)共52张,按顺序排放,依次黑桃(Spades)13张,红桃(Heart)13张,方块(Diamonds)13张,梅花(Clubs)13张。

随机抽取其中的四张,要求输出第几张什么牌,具体输出示例如下。

输出示例

Card number 16: 4 of Heart
Card number 30: 5 of Diamonds
Card number 8: 9 of Spades
Card number 4: 5 of Spades

难度

一般

 

 

public class DeckOfCards {
    public static void main(String[] args) {
        int[] deck = new int[52];
        String[] suits = {"Spades","Heart","Diamonds","Clubs"};
        String[] ranks = {"Ace","2","3","4","5","6","7","8","9","10",
        "Jack","Queen","King"};
        for (int i=0;i<deck.length;i++)
            deck[i] = i;
        for (int i = 0; i < deck.length; i++) {
            int index = (int)(Math.random() * deck.length);//Math.random()的数据类型为double
            int temp = deck[i];
            deck[i] = deck[index];
            deck[index] = temp;
        }
        for (int i = 0; i < 4; i++) {
            String suit = suits[deck[i] / 13];
            String rank = ranks[deck[i] % 13];
            System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);
        }
    }
}

 

posted @ 2020-03-13 15:35  哦呦aholic  阅读(1729)  评论(0编辑  收藏  举报