我的方法无法正常工作

时间:2016-05-25 13:13:02

标签: c# function methods logic

我正在制定一项计划,让人们在当天分配任务。

8个小时,我一直在努力弄清楚我的方法有什么问题。它 应该列出一份名单,一份任务清单和一个24小时的柜台,并在X小时为每个人分配一个任务。如果该任务需要不止一个人,它也会尝试分配下一个人(如果有的话)不是任何阻止该人被分配到任务的条件。)

在分配了列表中的所有人员后,程序应重置要分配的人员列表,并再次开始分配。

当我运行我的代码时,只有一轮分配,表的其余部分留空。

我试着搞清楚逻辑上有什么不对,但我只是不知道而且这太糟糕了!所以请帮帮我吧!

代码:

public string[,] AssignPersonsToMission()
{
    bool containCondition; // any item of Person.Conditions is in mission.Conditions?
    int assignedCounter = 0; // how many people assigned to a mission at given hour
    string[,] table = new string[missions.Count, 24]; // columns = missions, rows = hour of day
    List<Person> personsToAssign = persons; // filling list with people to assign
    List<Person> assignedPersons = new List<Person>(); // a list of people that have been assigned

    for (int mission = 0; mission < missions.Count; mission++) //go throught columns
    {
        for (int hour = 0; hour < 24; hour++) //go through rows
        {
            if (personsToAssign.Count == 0) //if all the people have been assigned to missions
            {
                personsToAssign = persons; // refill personsToAssign list with original list of people
                assignedPersons.Clear(); // reset assigned Persons list
            } 
            assignedCounter = 0; // reset assignedCounter
            foreach (Person person in personsToAssign.ToList()) //go through each person that can be assigned
            {
                containCondition = missions[mission].Conditions.Intersect(person.Conditions).Any(); // is there any condition in mission that the person has? 
                                                                                                    //if yes - that person cannot be assigned to the mission
                if (!containCondition)
                {
                    table[mission, hour] += person.Name + "|"; //put person in mission at a given hour
                    assignedPersons.Add(person); //add the person to a list of assigned people
                    personsToAssign.Remove(person);//remove the person from the list of people to assign
                    assignedCounter++;//increament how many people assigned to this mission at this hour

                    if (assignedCounter < missions[mission].NumOfPeople) //if not enough people have been assigned to the mission
                    {
                        continue; //go to the next person to assign
                    }
                    else { break; }//go the the next hour

                }// END OF if (!containCondition)

            }//END OF foreach (Person person in personsToAssign.ToList())

        }//END OF  for (int hour = 0; hour < 24; hour++)


    }//END OF for (int mission = 0; mission < missions.Count; mission++)

    return table;
}

1 个答案:

答案 0 :(得分:1)

问题是您要更改原始列表persons

改变这个:

List<Person> personsToAssign = persons;

为:

List<Person> personsToAssign = new List<Person> (persons);

您没有复制列表,只需设置对它的引用即可。因此,如果您从personsToAssign中删除某个人,则还会将其从persons中删除,因为这两个变量都指向内存中的相同列表。