我的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