简体   繁体   中英

How to assign a new value from Method's Array to the Variable?

My main goal is to create a Method, where it is possible to enter a number, out of which a Method will choose some other numbers (according to the purpose) and combine them in Array.

I need this Array and its value to be flexible, so I decided to create a new variable, that is within the scope for both Container() and Main() methods. Then I assigned a value from Container() to optainer , but it didn't work ( foreach doesn't display all numbers from Container() , only the first one). Where is my problem?

        static int[] optainer;

        static void Container()
        {
            uint numb = uint.Parse(Console.ReadLine());
            for (int i = 1000000; i >= 1; i--)
            {
                if (numb % i == 0)
                {
                    optainer = new int[] { i };
                }

            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter num. from 1 to 1 000 000");
            Container();
            foreach (int iw in optainer)
            {
                Console.WriteLine(iw);
            }

            // Expected: few numbers according to the condition; Real result: 1 
        
    ```
   

You have always only one element in optainer, this line is the error optainer = new int[] { i }; you create always a new array with only one item and the last is always 1.

you can change in this way

static List<int> optainer = new List<int>();

        static void Main(string[] args)
        {
            Console.WriteLine("Enter num. from 1 to 1 000 000");
            Container();
            foreach (int iw in optainer)
            {
                Console.WriteLine(iw);
            }
        }

        static void Container()
        {
            uint numb = uint.Parse(Console.ReadLine());
            for (int i = 1000000; i >= 1; i--)
            {
                if (numb % i == 0)
                {
                    optainer.Add(i);
                }

            }
        }

I'm sure there's a sexier way to do this, but try this:

static void Container()
{
    uint numb = uint.Parse(Console.ReadLine());
    for (int i = 1000000; i >= 1; i--)
    {
        if (numb % i == 0)
        {
            int size = optainer.Length + 1;
            Array.Resize(ref optainer, size);
            optainer[size - 1] = i;
        }
    }
}

Everytime you write

optainer = new int[] { i };

you create a new list(you overwrite the old one) you would have to append to the array. Therefore you would need to know the size of the array. To save memory you should use something more dynamic like lists.

Here is an explanation how to add a value: Adding values to a C# array

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