Próbuję skopiować następującą pętlę while, ale różni się sposobem, w jaki oba przeplatają moje „karty”.
Oto moja obecna pętla while, która działa, moim celem jest przerobienie tej pętli, ale odwrócenie kolejności obu tablic card_force:
while (index >= 0) {
card_force[index--] = bottom_half[j--];
card_force[index--] = top_half[i--];
}
Oto pętla while, którą chcę zrobić:
while (index >= 0) {
card_force[index--] = top_half[i--];
card_force[index--] = bottom_half[j--];
}
Jeśli jednak istnieją obie pętle, oryginalna pętla działa, ale druga pętla while w ogóle nie działa, co powoduje zwrócenie wartości „null” w 52 liniach. Tam, gdzie potrzebuję, aby zwrócić kartę 27, następnie kartę 1, następnie kartę 26, następnie kartę 2 i tak dalej, i tak dalej.
Mam ogromny problem z tym, co wydaje się prostym rozwiązaniem.
Oto mój obecny kod:
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);
}
}
Po uruchomieniu kodu zwraca następujący wynik:
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
1 odpowiedź
Musisz zresetować wartość index
przed drugą pętlą. Pierwsza pętla zatrzymuje się, gdy index
wynosi -1
, a ta wartość jest nadal taka sama, gdy druga pętla ją sprawdza. W rezultacie druga pętla nawet nie działa.
Podobne pytania
Nowe pytania
java
Java to język programowania wysokiego poziomu. Użyj tego tagu, jeśli masz problemy z używaniem lub zrozumieniem samego języka. Ten tag jest rzadko używany samodzielnie i jest najczęściej używany w połączeniu z [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] i [maven].
while
używa tej samej zmiennejindex
, która została w pełni wykorzystana przez pierwszą pętlęwhile
. Tak więc druga pętla while nigdy nie zostanie wykonana icard_force2
nigdy nie przypisze żadnej wartości, jest to po prostu tablica z 52 elementami, które nigdy nie mają przypisanej żadnej wartości do każdego elementu, więc wszystkie 52 elementy tonull
. Zresetujindex
do 51 lub po prostu użyj pętlifor
javac
bez konieczności zmiany jednej rzeczy, a następnie uruchomiony po wywołaniu). Poza tym nadal pokazujesz oba przypadki, co nie ma sensu: dlaczego miałbyś oczekiwać, że druga pętla while w ogóle zadziała, jeśli nie resetujesz żadnego z indeksów?