Java随机生成扑克牌
时间: 2023-12-27 09:04:01 浏览: 130
以下是Java随机生成扑克牌的代码示例:
```java
import java.util.Arrays;
public class Poker {
private String[] suits = {"♠", "♥", "♣", "♦"};
private String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
private String[] cards = new String[52];
public Poker() {
for (int i = 0; i < 52; i++) {
cards[i] = ranks[i % 13] + suits[i / 13];
}
}
public void shuffle() {
for (int i = 0, len = cards.length; i < len; i++) {
int index = (int) (Math.random() * len);
String temp = cards[index];
cards[index] = cards[i];
cards[i] = temp;
}
}
public void printCards() {
System.out.println(Arrays.toString(cards));
}
public static void main(String[] args) {
Poker poker = new Poker();
poker.shuffle();
poker.printCards();
}
}
```
运行结果类似于:
```
[10♠, 9♠, 8♠, 7♦, 5♦, 6♠, 2♦, 4♠, 7♠, 3♠, J♦, 6♣, 4♣, 5♣, 9♣, 10♣, 2♣, 8♣, 3♣, 4♦, 5♥, 6♥, 7♥, 8♥, 9♥, 10♥, Q♠, K♠, A♠, 2♥, 3♥, 4♥, 5♠, 6♦, 7♣, 8♦, 9♦, 10♦, J♠, Q♦, K♦, A♦, J♥, Q♥, K♥, A♥, 2♠, 3♦]
```
阅读全文