2011-04-30 270 views
0

我已經寫了一個甲板類,現在必須洗牌,然後打印出幾隻手來檢查它是如何工作的。然而,它似乎沒有洗牌,而是給出了完全相同的套牌。試圖洗牌「甲板」類,似乎沒有洗牌

public void makeHands() { 
    Deck deck = new Deck(); 
    Deck shuffled = shuffleDeck (deck); 
    printDeck (subdeck (shuffled, 0, 4)); 
    printDeck (subdeck (shuffled, 5, 9)); 
    printDeck (subdeck (shuffled, 10, 14)); 
    printDeck (subdeck (shuffled, 15, 19)); 
} 

public static int randomInt (int length, int i) { 
    double x = Math.random() * length; 
    int g = (int) x; 
    return g; 
} 

public Deck shuffleDeck (Deck deck) { 
    for (int i=0; i<deck.cards.length; i++) { 
     int g = randomInt (deck.cards.length, i); 
     swapCards (i, g); 
    } 
    return deck; 
} 
} 


public void swapCards (int first, int swap) { 
     Card temp = cards[first]; 
     cards[first] = cards[swap]; 
     cards[swap] = temp; 
} 
+4

功課? ..... – Prescott 2011-04-30 03:00:22

回答

2

該代碼看起來不完整。

看起來您需要將deck對象傳遞給swapCards方法。

swapCards()指的是卡片數組。目前尚不清楚這個數組在哪裏聲明。相反,你應該將甲板傳遞給這個方法並交換deck.cards [first]和deck.cards [swap]。