Card Force Trick. How to complete the perfect shuffle or riffle shuffle

ⅰ亾dé卋堺 提交于 2019-12-20 05:38:13

问题


I have completed nearly all of the java code for the perfect shuffle. I am just struggling with the error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 26 out of bounds for length 26 at cards_shuffle.main(cards_shuffle.java:72)" This error refers to the line:



Output:

Top Half of the Shuffled Deck:

4 of Spades 10 of Diamonds 8 of Spades Ace of Diamonds 4 of Hearts Jack of Hearts Queen of Hearts Queen of Spades 4 of Diamonds Jack of Spades King of Hearts 5 of Hearts 10 of Hearts 9 of Spades 2 of Clubs 6 of Spades King of Diamonds 3 of Clubs 7 of Spades Jack of Diamonds 3 of Hearts 7 of Diamonds 3 of Diamonds 9 of Hearts Ace of Clubs 5 of Diamonds

Bottom Half of the Shuffled Deck:

6 of Hearts 7 of Hearts 10 of Clubs Ace of Hearts 2 of Hearts Queen of Diamonds 6 of Clubs 8 of Diamonds King of Spades 5 of Spades Queen of Clubs 9 of Clubs 2 of Spades 3 of Spades 9 of Diamonds Ace of Spades 10 of Spades King of Clubs 7 of Clubs 2 of Diamonds 6 of Diamonds 4 of Clubs 8 of Hearts 5 of Clubs 8 of Clubs Jack of Clubs

Card Shuffle:

6 of Hearts

I have just included the 6 of Hearts once but it prints out 52 times.

Any help with this error would be greatly appreciated. 

回答1:


Your i variable exceeds the array bounds of top_half, since it is incremented in inner loop for bottom_half.length times. Also, you should not increment variable i at all, since you want to fill card force from top to bottom. Otherwise you should use separate index for it.

Try

int i = top_half.length - 1;
int j = bottom_half.length - 1;
int index = 51;
while (index >= 0) {
    card_force[index--] = top_half[i--];
    card_force[index--] = bottom_half[j--];
}


来源:https://stackoverflow.com/questions/59302097/card-force-trick-how-to-complete-the-perfect-shuffle-or-riffle-shuffle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!