【问题标题】:How to return Average number of rolls from a dice?如何从骰子返回平均掷骰数?
【发布时间】:2011-12-07 08:37:13
【问题描述】:

我需要显示获得 6 所需的平均掷骰数,以及该平均值所基于的 6 数。 我认为我遇到的问题是这部分代码?所以我想要我认为我拥有的平均卷数作为变量 AVGroll。平均值所基于的六位数应该是 loopcount 变量。

        AVGroll = AVGroll + loopcount;
        average = AVGroll / loopcount;

尝试尽可能地注释我的代码以使其可读。

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Collections;

        namespace CE0721a
        {
            class Tut4_7
            {
                public void run()
                {
        // Random number generator 
        Random rndm = new Random();

        //declaring number for Random Number Generator
        int number;

       // average number of runs
        int average;

        //declaring loopcount starts at 1
        int loopcount = 1;

        //Average roll starts at 0
        int AVGroll = 0;

        //Variable if it continues
        int progcontinue;

        //Start lable
        Start:
        do
        {
            number = rndm.Next(6) + 1;
            Console.WriteLine(number);
            Console.ReadKey();

           if (number < 6)
            {
                loopcount++;
            }

        } while (number != 6);



        AVGroll = AVGroll + loopcount;
        average = AVGroll / loopcount;

        Console.WriteLine("The roll count is:");
        Console.WriteLine(loopcount);
        Console.WriteLine("average");
        Console.WriteLine(AVGroll);
        Console.WriteLine("press 1 to continue or 0 to exit");




        progcontinue = (int.Parse(Console.ReadLine()));

        if (progcontinue > 0)
        {

            loopcount = 1;
            goto Start;

        }
        else
        {


        }
    }
}
        }

【问题讨论】:

  • 您的变量名难以阅读,请尝试下划线或第二个单词大写,例如AVG_roll 或AvgRoll`。
  • 您的程序特别遇到什么问题?错误信息?行为不正确?如果它的行为不正确,您希望它如何表现?问题中应该包含的所有内容。我建议避免使用goto。改为使用while(progcontinue) { /* ... */ } 循环。
  • 你有没有考虑过平均卷数可能不是整数值?
  • 平均值是数组部分的总和除以部分的数量,您已经完成了一半。
  • @HenkHolterman 我已经发布了我尝试过的内容以及我认为我遇到困难的地方

标签: c# random average dice


【解决方案1】:

我对您的代码有点困惑,但我认为您使用了错误的变量来计算 AvgRoll。第一次运行 AVGroll 将始终是 1 个示例:

 AVGroll = AVGroll (0) + loopcount (5);
    average = AVGroll (5) / loopcount (5);

所以它将是 1

我认为你需要这样做:

     int timesContinued = 1;
        //Start lable
                Start:
                do
                {
                    number = rndm.Ne

xt(6) + 1;
                Console.WriteLine(number);
                Console.ReadKey();

               if (number < 6)
                {
                    loopcount++;
                }

            } while (number != 6);



            AVGroll = AVGroll + loopcount;
            average = AVGroll / timesContinued;

            Console.WriteLine("The roll count is:");
            Console.WriteLine(loopcount);
            Console.WriteLine("average");
            Console.WriteLine(AVGroll);
            Console.WriteLine("press 1 to continue or 0 to exit");




            progcontinue = (int.Parse(Console.ReadLine()));

            if (progcontinue > 0)
            {
                loopcount = 1;
                timesContinued++;
                goto Start;

            }

这样,您将滚动的总次数除以您按下继续的次数,我希望这是您想要的。

【讨论】:

  • 我认为这确实计算了滚动的平均值以获得 6。它仍然不是很好的编码(但这是原始问题的另一个问题)。
【解决方案2】:

你除错了:

    AVGroll = AVGroll + loopcount;
    average = AVGroll / loopcount;

您希望对试验次数进行平均。一个试验是你滚动直到你得到 6。 然后基于progcontinue 进行更多试验。

因此有一个额外的变量来计算试验并除以:

    int trial =  1;

    //...

    AVGroll = AVGroll + loopcount;
    average = AVGroll / trials;

    //...

    if (progcontinue > 0)
    {

        loopcount = 1;
        ++trials;
        goto Start;

    }

您还需要打印average,而不是AVGroll:

    Console.WriteLine("average");
    Console.WriteLine(AVGroll); //should be average

【讨论】:

  • loopcount 已经是试验次数……不过,为什么他将其添加到平均值中是个好问题。
  • 不,不是。仔细阅读:“获得 6 所需的平均滚动数是多少”。一个试验是“你滚动直到你得到 6,然后记录滚动的数量”——这就是循环计数。在这一点上,平均值是 loopcount / 1(因为有一次试验)。但是您可以继续尝试获得 6 并记录它需要的滚动数。问题是关于平均值。
  • 这看起来和我的解决方案一样,所以+1 ^^
  • 所以我尝试了您的解决方案,这就是我得到的。我已经展示了我认为它应该是红色的。 link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
相关资源
最近更新 更多