我写了一个Deck课程,现在必须将其洗牌,然后打印出几只手来检查它是如何工作的。然而,它似乎并没有改变任何东西,而是提供完全相同的套牌。
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;
}
答案 0 :(得分:2)
代码看起来不完整。
看起来你需要将deck对象传递给swapCards方法。
swapCards()指的是卡阵列。目前尚不清楚这个数组的声明位置。相反,你应该将deck传递给这个方法并交换deck.cards [first]和deck.cards [swap]。