小编Zai*_*ris的帖子

近空的Java For-Loop行为很奇怪

此代码按预期打印"平均运行次数:0.99864197"

import java.util.Random;

public class A {
    public static void main(String[] args) {
        int min = -30;
        int max = 1;
        test(min, max);
    }
    static void test(int min, int max){
        int count = 0;
        Random rand = new Random(0);
        for(int j = 0; j < 2097152; j++){
            int number = min + rand.nextInt(max-min+1);
            for(int i = 0; i < number; ++i) {
                System.out.print("");
                count++;
            }
        }
        System.out.println("Average Number of Runs: " + count/65536F);

    }
}
Run Code Online (Sandbox Code Playgroud)

此代码应打印相同的确切数字,但它会打印一个随机的负数.

import java.util.Random;

public class …
Run Code Online (Sandbox Code Playgroud)

java debugging jvm loops nested-loops

5
推荐指数
1
解决办法
234
查看次数

标签 统计

debugging ×1

java ×1

jvm ×1

loops ×1

nested-loops ×1