Search code examples
c#arraysdynamic-arrays

I want to add elements to an array - as the user inputs integers


Simply put - I'm starting out in C# and I'm trying to make a game right now (text based) where the computer generates a number between 20-30. Then the user takes a number from 1-3 away, then the computer takes a number between 1-3 away and the last person to take a number away to make 0 loses.

My question is - I want to setup an array for user answers, but I cannot figure out how to add an element to the array based on how many rounds it takes the user to finish the game. E.g. They could put in 1 a bunch of times and take their time - adding a lot of elements to the array, or they could do a bunch of 3s and make the game quick.

{
class Program
{
    static void Main(string[] args)
    {
        Random rnd = new Random();
        int randomStart = 0;
        randomStart = rnd.Next(20, 30);

        Console.WriteLine("The starting number is {0}", randomStart);
        Console.ReadLine();



    }
}

}

I've tried experimenting with array placement and ideas but I left them out of the code that I've put here because it may be easier for you to guide me by just adding to it.

Thanks for the help.


Solution

  • You can't change the dimensions of an array dynamically in C#. If you need to add values at run time you to use a different list type.

    You might consider a standard List (https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx) or an ArrayList (https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx).