gpt4 book ai didi

Java 数组算法,将 shuffle 类型从 "in-shuffle"交换到 "out-shuffle"时出错

转载 作者:行者123 更新时间:2023-12-01 19:16:01 29 4
gpt4 key购买 nike

我正在尝试复制以下 while 循环,但它们交错我的“卡片”的方式有所不同。

这是我当前有效的 while 循环,我的目标是重新创建此循环,但颠倒两个 card_force 数组的顺序:

while (index >= 0) {

card_force[index--] = bottom_half[j--];
card_force[index--] = top_half[i--];
}


这是我想要制作的 while 循环:

while (index >= 0) {
card_force[index--] = top_half[i--];
card_force[index--] = bottom_half[j--];

}

但是,如果两个循环都存在,则原始循环可以工作,但第二个 while 循环根本不工作,导致 52 行返回“null”。我需要它返回卡 27,然后返回卡 1,然后返回卡 26,然后返回卡 2,依此类推。

我在看似简单的修复过程中遇到了巨大的麻烦。

这是我当前的代码:


String[] card_force = new String[52];
String[] card_force2 = new String[52];
int i = top_half.length - 1;
int j = bottom_half.length - 1;
int index = 51;


// Card out shuffle
while (index >= 0) {

card_force[index--] = bottom_half[j--];
card_force[index--] = top_half[i--];
}
// Card in shuffle
while (index >= 0) {
card_force2[index--] = top_half[i--];
card_force2[index--] = bottom_half[j--];
}


// Card out shuffle print
System.out.println("\nCard Shuffle:\n");
for (String card_force1 : card_force) {
System.out.println(card_force1);
}


// Card in shuffle print
for (String card_force1 : card_force2) {
System.out.println(card_force1);
}


}

当代码运行时,它返回以下结果:


Card Shuffle:

Card number 1: King of Clubs
Card number 27: 10 of Spades
Card number 2: Jack of Clubs
Card number 28: King of Diamonds
Card number 3: 6 of Diamonds
Card number 29: 5 of Diamonds
Card number 4: Queen of Hearts
Card number 30: 4 of Hearts
Card number 5: 5 of Clubs
Card number 31: Queen of Diamonds
Card number 6: Ace of Hearts
Card number 32: 7 of Diamonds
Card number 7: 3 of Hearts
Card number 33: 9 of Hearts
Card number 8: 2 of Hearts
Card number 34: 10 of Diamonds
Card number 9: 10 of Clubs
Card number 35: Jack of Diamonds
Card number 10: 2 of Spades
Card number 36: 2 of Diamonds
Card number 11: 9 of Clubs
Card number 37: 2 of Clubs
Card number 12: 8 of Spades
Card number 38: 4 of Spades
Card number 13: 7 of Spades
Card number 39: Ace of Diamonds
Card number 14: 8 of Clubs
Card number 40: 9 of Spades
Card number 15: Jack of Hearts
Card number 41: 6 of Clubs
Card number 16: 3 of Diamonds
Card number 42: 7 of Hearts
Card number 17: 6 of Hearts
Card number 43: King of Hearts
Card number 18: 4 of Clubs
Card number 44: Ace of Clubs
Card number 19: 3 of Spades
Card number 45: 8 of Hearts
Card number 20: Ace of Spades
Card number 46: 5 of Hearts
Card number 21: 10 of Hearts
Card number 47: Jack of Spades
Card number 22: Queen of Clubs
Card number 48: 9 of Diamonds
Card number 23: Queen of Spades
Card number 49: 8 of Diamonds
Card number 24: 7 of Clubs
Card number 50: 5 of Spades
Card number 25: 4 of Diamonds
Card number 51: 6 of Spades
Card number 26: King of Spades
Card number 52: 3 of Clubs
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null

最佳答案

您需要在第二次循环之前重置index的值。当 index-1 时,第一个循环停止,并且当第二个循环检查该值时,该值仍然相同。结果,第二个循环甚至永远不会运行。

关于Java 数组算法,将 shuffle 类型从 "in-shuffle"交换到 "out-shuffle"时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59416290/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com