卡片力量技巧。如何完成 perfect shuffle 或 riffle shuffle

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

我已经完成了完美随机播放的几乎所有 java 代码。我只是在为错误而苦苦挣扎: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 26 out of bounds for length 26 at cards_shuffle.main(cards_shuffle.java:72)" 这个错误是指行:



Output:

已洗牌的上半部分:

黑桃 4 方块 10 黑桃 8 方片王牌 红心 4 红心杰克 红桃皇后 黑桃皇后 方块 4 黑桃杰克 红心国王 红心 5 红心 10 黑桃 9 2个俱乐部 黑桃 6 方块之王 3 俱乐部 黑桃 7 方块杰克 红心 3 方块 7 方块 3 红心 9 梅花王牌 方块 5

已洗牌的下半部分:

6 个红心 红心 7 10个俱乐部 红桃A 红心 2 钻石皇后 6个俱乐部 方块 8 黑桃K 黑桃 5 俱乐部女王 9个俱乐部 黑桃 2 黑桃 3 方块 9 黑桃王牌 黑桃 10 梅花之王 7 俱乐部 2 方块 方块 6 4个俱乐部 红心 8 5个俱乐部 8个俱乐部 梅花 J

洗牌:

6 个红心

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

Any help with this error would be greatly appreciated. 

您的 i 变量超出了 top_half 的数组边界,因为它在内部循环中递增了 bottom_half.length 次。 另外,你根本不应该增加变量 i,因为你想从上到下填充卡片力。否则你应该为它使用单独的索引。

尝试

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--];
}