简体   繁体   English

我的 java 循环在我调用 function 时提前完成,但在未调用 function 时正常工作

[英]My java loop is finishing early when I call a function but works normally when the function is not called

I am confused here.我在这里很困惑。 I am writing a program that simulates rolling a pair of dice.我正在编写一个模拟掷骰子的程序。 I have a nested for loop that includes a function call to calculate a value.我有一个嵌套的 for 循环,其中包括一个 function 调用来计算一个值。 When I comment out the function call my loop iterates the correct number of times.当我注释掉 function 调用时,我的循环会迭代正确的次数。 However, when I include the function call, the loop always terminates early.但是,当我包含 function 调用时,循环总是提前终止。 Can someone please help me explain why this happens?有人可以帮我解释为什么会这样吗?

public class RollDice {
    /** Simulates the rolling of pair of dice
     * 
     * @param num is an int between 2 and 12 inclusive
     * @return is the number of rolls it took to roll the value of num
     */
    static int roll(int num) {
        
        if (num <1 || num > 12) {  // illegal value passed
            throw new IllegalArgumentException("parameter must be between 1 and 12 inclusive");
        }
        
        int x, y;           // integers between 1 and 6 inclusive
        int count = 0;      // number of times dice were rolled
        
        /* roll dice once */
        x = ( int)(6 * Math.random());
        y = ( int)(6 * Math.random());
        
        while (x + y != num) {  // keep rolling dice until they equal num
            
            count++;
            /* roll dice again */
            x = ( int)(6 * Math.random());
            y = ( int)(6 * Math.random());
            
        }
        
        return count;
    }

    public static void main(String[] args) {
        
        /* output a header */
        
        System.out.printf("%10s  %40s", "Total on Dice", "Average Number of Rolls\n");
        System.out.println("---------------------------------------------------------");
        
        for (int num = 2; num <= 12; num++) {
            int total = 0;
            
            for (int i = 0; i <= 10000; i++) {
                total += roll(num); // num goes up to 10 when function is called instead of 12
            } // end inner loop
            
            System.out.println("num = " + num);
            
        } // end outer loop
        
    } // end main()
    
} // end class RollDice

This这个

( int)(6 * Math.random())

is a random number from 0 to 5. The reason your program stops showing any results when num gets to 11 is that x + y will never be greater than 10.是从 0 到 5 的随机数。当num达到 11 时程序停止显示任何结果的原因是x + y永远不会大于 10。

If you had如果你有

(int) (1 + 6 * Math.random())

it would give you a random number from 1 to 6, which I imagine is what you want.它会给你一个从 1 到 6 的随机数,我想这就是你想要的。

The code is not terminating the loop early, it's actually never returning from the function when num is 11. So your application is actually in an infinite loop and never completes.代码没有提前终止循环,当 num 为 11 时,它实际上永远不会从 function 返回。所以您的应用程序实际上处于无限循环中并且永远不会完成。

That's because you are generating random numbers from 0 to 5 (inclusive).那是因为您正在生成从 0 到 5(含)的随机数。 So they never add up to 11 and the function never exits.所以他们永远不会加起来 11 并且 function 永远不会退出。 Add 1 to the rolls and you'll find it now exits.将 1 添加到卷中,您会发现它现在退出了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM