每次循环重新启动时,使循环为随机值更改

时间:2018-07-16 20:22:59

标签: java loops random while-loop

我正在制作硬币翻转模拟器,但是随机值有问题。我需要创建一个随机值,每次while循环再次启动时,该值都会改变。

    System.out.println("How many times do I flip the coin?");
    int repetition;
    int heads = 0;
    int tails = 0;
        Scanner amount_of_times = new Scanner(System.in);
    repetition = amount_of_times.nextInt();
    double luck = (Math.random());
    do {
        if (luck > 0.5)
            heads++ ;
        else
            tails++ ;
        repetition --;
    } while (repetition > 0);
    System.out.println("The amount of heads was " + heads + ", meanwhile the amount of tails was " + tails + ".");
        amount_of_times.close();
    }
}

1 个答案:

答案 0 :(得分:1)

在循环前 ,您只能为max()分配一个值。如果希望它在每次迭代中获得不同的值,则应在循环的内部中将赋值移至该赋值:

((x+y-z)+(min(min(x,y),z)+avg(x,y))