java写从一副牌中不放回抽取两张牌
时间: 2023-05-28 21:07:19 浏览: 160
以下是一种实现方式:
```java
import java.util.ArrayList;
import java.util.Collections;
public class DrawCards {
public static void main(String[] args) {
// 创建一副牌
ArrayList<String> cards = new ArrayList<>();
String[] suits = {"♠", "♥", "♦", "♣"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String suit : suits) {
for (String rank : ranks) {
cards.add(rank + suit);
}
}
// 洗牌
Collections.shuffle(cards);
// 抽取两张牌
String card1 = cards.remove(0);
String card2 = cards.remove(0);
System.out.println("抽到的两张牌为:" + card1 + " 和 " + card2);
}
}
```
运行结果示例:
```
抽到的两张牌为:7♦ 和 6♠
```
阅读全文