简体   繁体   中英

How do I receive the average of the sum from my dice rolls?

I have an assignment about lists and methods where I need to get the average from my dice rolls, but I get errors and can't figure out how to do it another way. I'm only allowed to use static for this, not private or anything else: the method

    class Program
    {
        // detta är en statisk metod med en int som ett returvärde
        // metoden tar en parameter i form av ett random objekt av
        // randomklassen
        static int RullaTärning(Random slumpObjekt)
        {
            // här ska du skapa kod som slumpar fram ett tal
            // mellan 1 och 6, så att metoden "rullar" en 6 sidig
            // tärning när vi kallar på den

            // metoden ska sedan returnera det rullade värdet
            int Nr = slumpObjekt.Next(1,7);
            return Nr;
        }

the problem at case 2:

  ```
            switch (val)
            {
                case 1:
                    Console.Write("\n\tHur många tärningar vill du rulla: ");
                    bool inmatning = int.TryParse(Console.ReadLine(), out int antal);

                    if (inmatning)
                    {
                        for (int i = 0; i < antal; i++)
                        {
                            // här kallar vi på metoden RullaTärning
                            // och sparar det returnerade värdet i 
                            // listan tärningar
                            tärningar.Add(RullaTärning(slump));
                            
                        }
                    }
                    break;
                case 2:
                    int sum = 0;
                    // Skapar en int som ska innehålla medelvärdet av alla tärningsrullningar.
                    if (tärningar.Count <= 0)
                    {
                        Console.WriteLine("\n\tDet finns inga sparade tärningsrull! ");
                    }
                    else
                    {
                        Console.WriteLine("\n\tRullade tärningar: ");
                        foreach (int tärning in tärningar)
                        {
                            Console.WriteLine("\t" + tärning);
                        }
                        sum = tärningar / antal; //Doesn't work, CS0019, CS0165
                        Console.WriteLine("\n\tMedelvärdet på alla tärningsrull: " + sum); // Här ska medelvärdet skrivas ut (AVERAGE HERE)
                    }

                    break;
Do I need to add anything in the method or is that just for case 1?

You can't divide the entire list ( List<int> ) by some value:

// Compile Time error
sum = tärningar / antal

To find out the average manually (in real life we put var avg = tärningar.Average(); ) you should sum the items and divide the sum (not list!) but Count :

// Tven if items of list are int, 
// the average will be floating point (in general case)
// In order to avoid integer division: 5 / 4 = 1, not 1.25
// let declare sum as a floating point value  
double sum = 0.0;

foreach (var tärning in tärningar)
  sum = sum + tärning;

// try avoiding magic constants, variables etc. 
// Here we must divide by tärningar.Count - numbers of items in the list 
double average = sum / tärningar.Count; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM