有人在群里讨论发牌的方法,我参与了讨论,其实很早就想写一个了,但一直懒的动地方,今天用了快40分写了一个,不知道对不对。呵呵。 PS:没有大小王 import java.util.Random; public class CardPoker ... { public static void main(String[] args) ...{ Random r = new Random(); int[][] user = new int[4][13]; int tmp; boolean flag = true; boolean[] card = new boolean[52]; // 初始化flag数组 for (int i = 0; i < 52; i++) ...{ card[i] = true; } try ...{ for (int i = 0; i < 4; i++) ...{ for (int j = 0; j < 13; j++) ...{ while (flag) ...{ tmp = r.nextInt(52); if (card[tmp]) ...{ card[tmp] = false; // 证明此牌已经发出 user[i][j] = tmp; flag = false; } } flag = true; } } } catch (ArrayIndexOutOfBoundsException e) ...{ e.printStackTrace(); } for (int i = 0; i < 4; i++) ...{ System.out.print("第" + (i + 1) + "选手的牌是"); for (int j = 0; j < 13; j++) ...{ System.out.print(" " + user[i][j]); } System.out.println(); } }}