【问题标题】:My method doesn't work as expected我的方法没有按预期工作
【发布时间】:2016-05-25 13:13:02
【问题描述】:

我正在开发一个计划,将人们分配到全天的任务中。

8 个小时以来,我一直在努力找出我的方法出了什么问题。它 应该获取一份姓名列表、一份任务列表和一个 24 小时计数器,并在 X 小时为每个人分配一个任务。如果该任务需要多人,它也会尝试分配下一个(如果有不是阻止该人被分配到任务的任何条件)。

列表中的所有人员都已分配后,程序应重置要分配的人员列表,并重新开始分配。

当我运行我的代码时,只有 1 轮分配,表格的其余部分是空白的。

我试图弄清楚逻辑有什么问题,但我就是不能,这太令人沮丧了!!所以请帮帮我!

代码:

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;
}

【问题讨论】:

  • 你真的应该考虑选择一个能概括具体问题的标题。以便其他人在遇到相同问题时可以找到您的帖子。 Help center (stackoverflow.com/help/how-to-ask) 在这里
  • 因此,当您使用调试器单步执行时,您不能说出为什么没有填充表吗?

标签: c# function methods logic


【解决方案1】:

问题是你正在改变你原来的列表persons

改变这个:

List<Person> personsToAssign = persons;

到:

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

您没有制作列表的副本,您只是设置了对它的引用。所以如果你从personsToAssign 中删除一个人,你也会从persons 中删除它,因为两个变量都指向内存中的同一个列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多