import java.util.*; import java.io.*; public class Playingcards { public static void main(String[] args) { ArrayList <String> color = new ArrayList<String>();//扑克牌的颜色 color.add("黑桃"); color.add("红心"); color.add("方块"); color.add("梅花"); ArrayList <String> number = new ArrayList<String>();//扑克牌的数字 for(int i = 3; i <= 10; i ++) { number.add(i + ""); } number.add("J"); number.add("Q"); number.add("K"); number.add("A"); number.add("2"); HashMap<Integer,String> map = new HashMap<Integer,String>();//生成54张牌 int index = 0; for(String num : number) { for(String col : color) { map.put(index++, col + num); } } map.put(index++,"小王"); map.put(index++,"大王"); ArrayList <Integer> card = new ArrayList<Integer>();//card代表54张牌的顺序; for(int i = 0; i <= 53; i ++) { card.add(i); } Collections.shuffle(card);//将牌的顺序打乱 ArrayList<Integer> player1 = new ArrayList<Integer>(); ArrayList<Integer> player2 = new ArrayList<Integer>(); ArrayList<Integer> player3 = new ArrayList<Integer>(); ArrayList<Integer> surplus = new ArrayList<Integer>(); for(int i = 0; i < card.size(); i ++) { if(i >= 51) { surplus.add(card.get(i)); } else { if(i % 3 == 0) { player1.add(card.get(i)); } else if(i % 3 == 1) { player2.add(card.get(i)); } else { player3.add(card.get(i)); } } } Collections.sort(player1);//将发的牌排序; Collections.sort(player2); Collections.sort(player3); ArrayList<String> splayer1 = new ArrayList<String>(); ArrayList<String> splayer2 = new ArrayList<String>(); ArrayList<String> splayer3 = new ArrayList<String>(); ArrayList<String> ssurplus = new ArrayList<String>(); for(Integer key : player1) { splayer1.add(map.get(key)); } for(Integer key : player2) { splayer2.add(map.get(key)); } for(Integer key : player3) { splayer3.add(map.get(key)); } for(Integer key : surplus) { ssurplus.add(map.get(key)); } System.out.println("玩家1" + splayer1); System.out.println("玩家2" + splayer2); System.out.println("玩家3" + splayer3); System.out.println("底牌" + ssurplus); } }