为什么这个Java程序会终止?

时间:2017-02-14 21:34:11

标签: java loops

作为一项作业,我必须编写一个程序,模拟一次处理五张牌,直到发出皇家同花顺。这样做10次,然后取平均花费多少次来处理皇家同花顺。我写了一个运行良好的程序,但没有说明单个套牌不会有一张卡片中的两张,因此处理皇家同花顺所花费的次数超过200,000。我想我修好了,但现在我没有得到任何输出,因为程序没有终止。我是否无意中无限循环?

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

}

4 个答案:

答案 0 :(得分:1)

你没有在外部while循环中更改card1,card2,card3,card4,card5的值。基本上第一次你要将它们设置为所有不同的东西(如你所愿)但是在每次后续迭代中它们都不会用新的随机值更新,因为它们已经满足它们都不同的条件!

答案 1 :(得分:0)

你必须改变flush的值来打破while循环。

答案 2 :(得分:0)

您需要添加以下行

card1 = card2 = card3 = card4 = card5 = 0;

在以下地方:

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++;
    }
    card1 = card2 = card3 = card4 = card5;
}

对外输出:

  

平均手数为177968.4

请注意,您目前只检查直接冲洗,没有皇室冲洗。对于皇室冲洗,你的内在逻辑应该是:

if (max - min == 4) {
    if ((13 == max) || (26 == max) || (39 == max) || (52 == max)) {
        flush = true;
    } else {
        i++;
    }
} else {
    i++;
}

对外输出

  

平均手数为4944953.0

答案 3 :(得分:-1)

您的根本问题之一是您的随机数生成器。您是否想知道为什么在所有卡片的每次运行中总是会得到相同的精确值?这是因为Random类是基于您传入的初始种子的“伪随机”。在您的代码中,每次运行都会传入1,导致您的卡每次都具有相同的精确值。要获得更“随机”的值,请使用System.currentTimeMillis()作为初始种子。