【发布时间】:2015-03-02 09:21:59
【问题描述】:
以下代码在游戏中洗牌。我必须解释代码在演示文稿中的作用。我是Java编码的新手,有谁能给我逐行解释以下代码:
// Deck shuffling method
public void shuffleDeck() {
//Seed the Random instance with nanoTime
Random random = new Random(System.nanoTime());
for(int i = 0; i < 52; i++) {
int swapIndex = random.nextInt(52);
if (swapIndex != i) {
PlayingCard temp = cardDeckArray[i];
cardDeckArray[i] = cardDeckArray[swapIndex];
cardDeckArray[swapIndex] = temp;
}
}
cardIndex = 0; //Next card to be pulled off the deck
}
【问题讨论】:
-
我投票决定重新开放。我不明白为什么它“太宽泛”了,我也明白新手程序员很容易被反复的交换弄糊涂。