Link to home
Start Free TrialLog in
Avatar of skij
skijFlag for Canada

asked on

C# - Add number to the end of list for each item

Using C#, in the example below, how can I add a number to the end of a list for each of the items?

For example, a random number should be added to the list of Lucky Numbers for each Cat.

using System;
using System.Linq;
using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot()]
    public class DemoExample : Robot
    {
        List<Cat> myCats = new List<Cat> 
        {
            new Cat 
            {
                Name = "Sylvester",
                Age = 8,
                LuckyNumbers = {1,2,3}
            },
            new Cat 
            {
                Name = "Whiskers",
                Age = 2,
                LuckyNumbers = {4,5,6}

            },
            new Cat 
            {
                Name = "Sasha",
                Age = 13,
                LuckyNumbers = {7,8,9}
            }
        };

        protected override void OnTick()
        {

                Print("---------------");
                Random rnd = new Random();
                foreach(Cat theCat in myCats)
                {
                  Print("The average of all the lucky numbers for {0} is : {1} ", theCat.Name, theCat.LuckyNumbers.Average());
                  // Add a luck number for the cat here -- Math.Round(rnd.NextDouble() * 50)  

                }

        }

    }
}

public class Cat
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<double> LuckyNumbers = new List<double>();
}

Open in new window

SOLUTION
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skij

ASKER

Thank you both, I am using parts from both your ideas.