私は、ロイヤルフラッシュが処理されるまで、一度に5枚のカードを扱うことをシミュレートするプログラムを書く必要があります。これは10回行い、ロイヤルフラッシュを処理するのにかかる回数の平均をとっています。私はうまく動作するプログラムを書いたが、1つのデッキに2枚のカードがないという事実を考慮していないので、ロイヤルフラッシュを処理するのにかかった回数は20万回を超えていた。私はそれを修正したと思うが、今はプログラムが終了していないので、出力が得られない。無意識のうちに無限ループを作りましたか?このJavaプログラムはなぜ終了しませんか?
import java.util.Random;
public class Problem1Version2 {
public static void main(String[] args) {
// This program simulates the dealing of poker hands and returns the average
// over 10 rounds of how many hands are dealt until a royal straight flush is received.
// Initialize flush to false, which indicates that a royal straight flush has not
// yet been dealt. Set the count variable, i, to 1.
boolean flush = false;
int i = 1;
// Initialize the cards to 0 before any cards are dealt.
int card1 = 0, card2 = 0, card3 = 0, card4 = 0, card5 = 0;
// Initialize the total number of tries across all experiment rounds to 0.
int totalTries = 0;
// Repeat experiment for 10 seeds.
for (int j = 0; j < 10; j++) {
// The round number is the seed.
Random r = new Random(j);
// Deal hands of 5 until a royal straight flush is dealt.
while (flush == false) {
// To simulate the use of one deck, only one of each card can be dealt in a hand.
while (card1 == card2 || card1 == card3 || card1 == card4 || card1 == card5 ||
card2 == card3 || card2 == card4 || card2 == card5 || card3 == card4
|| card3 == card5 || card4 == card5) {
// Cards 1-13 are the hearts, 14-26 the spades, 27-39 the clubs, and 40-52 the diamonds.
card1 = r.nextInt(52) + 1;
card2 = r.nextInt(52) + 1;
card3 = r.nextInt(52) + 1;
card4 = r.nextInt(52) + 1;
card5 = r.nextInt(52) + 1;
}
// Calculate the max and min of the cards.
int max = Math.max(card1, Math.max(card2, Math.max(card3, Math.max(card4, card5))));
int min = Math.min(card1, Math.min(card2, Math.min(card3, Math.min(card4, card5))));
// Check if the hand dealt is a royal straight flush by confirming that
// they span 5 consecutive numbers (this is true if the difference between
// the max and min is 4 since they are all different), and that they all come
// from the same suit (1-13, 14-26, 27-39, or 40-52).
if (max - min == 4) {
if ((1 <= min) && (13 >= max)) {
flush = true;
}
else if ((14 <= min) && (26 >= max)) {
flush = true;
}
else if ((27 <= min) && (39 >= max)) {
flush = true;
}
else if ((40 <= min) && (52 >= max)) {
flush = true;
}
else {
i++;
}
}
else {
i++;
}
}
// Add count from last round to total number of tries over the 10 rounds.
totalTries += i;
// Reset flush to false for the while loop.
flush = false;
}
// Print the average number of required tries.
System.out.println("The average number of hands it took was " + totalTries/10.0);
}
}
デバッグ、デバッグおよび1より多くの時間をデバッグした:ロイヤルフラッシュのために、あなたの内側の論理がなければなりません! – Andremoniy
私は行ごとに行っており、すべてが期待通りになります。私は決してそれが王室の潮流に達することはない。 – jillianjmb
enumを使ってカードを表現し、 'List deck'を' Collection.shuffle(deck) 'とすることを考えてみましょう。これにより、コードが単純化され、デバッグが容易になります。 –