我有7名员工的名单.我正在迭代当前月份的日期循环,并希望在每个日期分配两名员工,但在周末,他们不应重复,直到所有员工都被分配.例如:我有七名员工:
John
Sophia
Olivia
Davis
Clark
Paul
Thomas
现在我的日期循环是:
for (int i = 0; i < dates.Length; i++)
{
DateTime newDate = new DateTime();
newDate = dates[i];
/*if(newdate == "Saturday")
var EmpName1 = emplist[i];
var EmpName2 = emplist[i];*/
}
在上面的循环中,我希望在周六和周日分配两名员工,直到之前没有分配任何其他员工.像这样的东西:
4th March: John and Sophia
5th March: Olivia and Davis
11th March: Clark and Paul
12th March: Thomas and John
等等……直到所有人都被分配,才会被分配.之后,列表将重新开始.谁可以帮我这个事?
最佳答案 每次需要选择一个时,请为该人选择单独的索引.
选择后,更改索引:
index = (index + 1) % employees.Length // Number fo employees
%(表示模数)确保在达到employees.Length时计数器再次从0开始.
所以类似于:
var empIndex = 0;
for (int i = 0; i < dates.Length; i++)
{
DateTime newDate = new DateTime();
newDate = dates[i];
if(newdate == "Saturday") // and Sunday, use or: || (newData == "Sunday"))
{
var EmpName1 = emplist[empIndex];
empIndex = (empIndex + 1) % empList.Length;
var EmpName2 = emplist[empIndex];
empIndex = (empIndex + 1) % empList.Length;
}
}