0

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


        }
    }
}
        }
4

2 回答 2

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
于 2011-12-07T08:47:13.163 回答
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;

            }

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

于 2011-12-07T08:44:44.830 回答