首页 > 解决方案 > 为什么返回数组在c#的函数中不起作用

问题描述

我正在为学校编写一个项目,但在编写代码时遇到了问题。我试图在一个数组中返回 2 个整数,但它对我不起作用......有人可以帮助我吗?

public static int[] generation(char[,] people)
    {

        int neighbors = 0;
        int[] changes = new int[2];
        changes[0] = 0; //alive
        changes[1] = 0; // dead
        for (int i = 0; i < people.GetLength(0); i++)
        {
            for (int j = 0; j < people.GetLength(1); j++)
            {
                neighbors = cell_state(i, j - 1, people) + cell_state(i, j + 1, people) + cell_state(i + 1, j, people) + cell_state(i - 1, j, people) + cell_state(i - 1, j - 1, people) + cell_state(i + 1, j + 1, people) + cell_state(i - 1, j + 1, people) + cell_state(i + 1, j - 1, people);
                if (neighbors <= 1)
                {
                    people[i, j] = '+';
                    changes[1] =+ 1;
                }
                if (neighbors > 3)
                {
                    people[i, j] = '+';
                    changes[1] =+ 1;

                }
                if (neighbors == 3)
                {
                    people[i, j] = '0';
                    changes[0] =+ 1;
                }
            }
        }
        return changes;

    }

然后我主要写了这个:

    int[] changes = new int[2];
    changes = generation(people);
                born_counter = changes[0];
                died_counter = changes[1];

有人可以帮助我吗?

标签: c#functionreturn

解决方案


推荐阅读