52张牌随机抽4张牌

public class BookDemo7_2 {
public static void main(String[] args) {
// 把52张牌存入数组
int[] deck = new int[52];
// 定义花色
String[] suits = { “Spades”, “Hearts”, “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); // 随机产生一个[1~52)的角标
        int temp = deck[i];
        deck[i] = deck[index]; // deck[i]为任意一个值
        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("牌号 "+deck[i] + "  " + rank + "  " + suit);
    }
}

}

你可能感兴趣的:(52张牌随机抽4张牌)