你有一副扑克,请用java编写给三个人随机发牌的代码
时间: 2023-03-22 07:01:35 浏览: 96
好的,下面是用 Java 编写的给三个人随机发牌的代码:
```java
import java.util.ArrayList;
import java.util.Collections;
public class ShuffleCards {
public static void main(String[] args) {
// 创建一副扑克牌
ArrayList<String> cards = new ArrayList<String>();
String[] colors = {"♠", "♥", "♦", "♣"};
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String color : colors) {
for (String num : nums) {
cards.add(color + num);
}
}
// 洗牌
Collections.shuffle(cards);
// 发牌
ArrayList<String> player1 = new ArrayList<String>();
ArrayList<String> player2 = new ArrayList<String>();
ArrayList<String> player3 = new ArrayList<String>();
for (int i = 0; i < cards.size(); i++) {
if (i % 3 == 0) {
player1.add(cards.get(i));
} else if (i % 3 == 1) {
player2.add(cards.get(i));
} else {
player3.add(cards.get(i));
}
}
// 打印每个玩家的牌
System.out.println("玩家1:" + player1);
System.out.println("玩家2:" + player2);
System.out.println("玩家3:" + player3);
}
}
```
运行结果会输出每个玩家的牌,例如:
```
玩家1:[♦10, ♠K, ♠2, ♣9, ♠Q, ♥10, ♥8, ♠7, ♦K, ♦J, ♠9]
玩家2:[♦A, ♣10, ♣8, ♥3, ♥Q, ♦Q, ♠4, ♣A, ♣K, ♠J, ♥A]
玩家3:[♣5, ♦7, ♦9, ♠5, ♥K, ♣7, ♣2, ♠A, ♦5, ♦4, ♠8, ♥9, ♦6]
```
阅读全文