我的CompSci类有一个我们正在做的工作,我们必须打印出一副纸牌作为一个二维的6行8列数组。每个“卡”基本上是一个随机生成的数字(1-12)和随机选择的套装(钻石,心,黑桃,&俱乐部);阵列中的任何地方都不能重复卡片。这是我的代码:如何打印出没有重复的随机二维数组?
static Random random = new Random(1234567);
static int i = 1;
static int a;
static int d;
static List<String> suits = new LinkedList<String>();
static List<String> cards = new LinkedList<String>();
static int[][] grid = new int[6][8];
public static void main(String[]args)
{
suits.add("Diamonds");
suits.add("Clubs");
suits.add("Hearts");
suits.add("Spades");
cards.add("1");
cards.add("2");
cards.add("3");
cards.add("4");
cards.add("5");
cards.add("6");
cards.add("7");
cards.add("8");
cards.add("9");
cards.add("10");
cards.add("11");
cards.add("12");
drawGrid();
}
private static void drawGrid()
{
for(int b = 0; b < grid.length; b++)
{
for(int c = 0; c < grid[i].length; c++)
{
a = (int)(Math.floor(suits.size() * Math.random()));
d = (int)(Math.floor(suits.size() * Math.random()));
System.out.print(" |" + cards.get(d) + " " + suits.get(a) + "|");
Collections.shuffle(suits);
Collections.shuffle(cards);
}
System.out.println();
}
}
当前代码有什么问题?它工作与否? – SMA 2014-11-25 14:38:05
只要你知道每件衣服有13件。 Ace - > 10 Jack Queen King – 2014-11-25 14:42:29
为什么你有一个静态的'Random'对象,然后到处使用'Math.random()'? – 2014-11-25 14:42:49