如何返回骰子的平均卷数?

时间:2011-12-07 08:37:14

标签: c# random average dice

我需要显示获得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
        {


        }
    }
}
        }

2 个答案:

答案 0 :(得分:1)

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

 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;

            }

通过这种方式,您可以按照您按下的次数划分总时间,我希望这是你想要的。

答案 1 :(得分:1)

你分错了:

    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