c# - 试图找到 Tom 在 : 10 repetitions 上掷出 6 所需的平均尝试次数

标签 c# for-loop average dice

它需要滚动的总数并将它们除以 10。例如,如果需要 56 个滚动,那么我的平均值是 5.6。

        Random numGen = new Random ();

        int numOfAttempt = 0;
        int Attempt = 0;
        int avrBefore = 0;
        int avrAfter = 0;


        for (int i = 1; i <= 10; i++)

            do {

                Attempt = numGen.Next (1, 7);
                Console.WriteLine (Attempt);
                numOfAttempt++;
                avrBefore = numOfAttempt;
                avrAfter = avrBefore / 10;

            } while (Attempt != 6); 

        Console.WriteLine ("He tried " + numOfAttempt + " times to roll a six.");
        Console.WriteLine ("The average number of times it took to get a six was " + avrAfter);
        Console.ReadKey ();

最佳答案

也许您需要在 while 之前重置 for 循环内的变量:

    for (int i = 1; i <= 10; i++){
    int numOfAttempt = 0;
    int Attempt = 0;
    int avrBefore = 0;
    int avrAfter = 0;

        do {//your code

关于c# - 试图找到 Tom 在 : 10 repetitions 上掷出 6 所需的平均尝试次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30281435/

相关文章:

c# - 指向 Byte[] 数组的不安全 Int32 指针

java - 增强了对象的 for 循环和 null 检查

for 循环内的 JavaScript setTimeout 函数仅发生一次

c - 为什么我的 for 循环适用于 "&&"而不适用于 ","

c# - 在 C# 中定义有界泛型类型参数

c# - 通过多个值查询字段的 Linq 语句被转换为 in 语句

c# - 删除 Entity Framework 中的多对多关系对象

mysql 复杂 SUM 除以 COUNT

python 列出 bin 大小的平均值

javascript - 计算任意数量参数的平均值