Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将列表安排到预构建的模板?(控制台)_C#_List_Console - Fatal编程技术网

C# 如何将列表安排到预构建的模板?(控制台)

C# 如何将列表安排到预构建的模板?(控制台),c#,list,console,C#,List,Console,我有一个通用的名称列表,其编号取决于用户选择的内容。我想把这些名字安排在一天中用户输入的时间范围内。 例如:我们有3个名字,用户输入08:00-17:00。从08:00-17:00我们有9个小时->9/3=3,所以在3个小时的模板中安排名称 这是它在代码中的外观: List<string> myList = new List<string>(); myList.Add(Name1); myList.Add(Name2); myList.Add(Name3); Conso

我有一个通用的名称列表,其编号取决于用户选择的内容。我想把这些名字安排在一天中用户输入的时间范围内。 例如:我们有3个名字,用户输入08:00-17:00。从08:00-17:00我们有9个小时->9/3=3,所以在3个小时的模板中安排名称

这是它在代码中的外观:

List<string> myList = new List<string>();
myList.Add(Name1);
myList.Add(Name2);
myList.Add(Name3);

Console.WriteLine("Enter hours range: ");
Console.ReadLine(); //in this case the user input is 08:00-17:00

if (myList.Count == 3)
{
    Console.WriteLine("8-11 " + myList.ElementAt(0)); //Name1
    Console.WriteLine("11-14 " + myList.ElementAt(1)); //Name2
    Console.WriteLine("14-17 " + myList.ElementAt(2)); //Name3
}
List myList=new List();
myList.Add(名称1);
myList.Add(Name2);
myList.Add(Name3);
Console.WriteLine(“输入小时范围:”);
Console.ReadLine()//在这种情况下,用户输入为08:00-17:00
如果(myList.Count==3)
{
Console.WriteLine(“8-11”+myList.ElementAt(0));//名称1
Console.WriteLine(“11-14”+myList.ElementAt(1));//名称2
Console.WriteLine(“14-17”+myList.ElementAt(2));//Name3
}
问题来了,当有3个以上的名字,时间减半(假设17:30)


关于如何做这类事情有什么想法吗?

你可以这样做

private static void CalculateTimeIntervals()
    {
        var startTime = new DateTime(2014, 8, 15, 8, 0, 0);
        var endTime = new DateTime(2014, 8, 15, 17, 0, 0);
        var lengthOfTime = endTime - startTime;
        var numberOfPeople = 4;
        var dividedLengthOfTime = lengthOfTime.Ticks / numberOfPeople;
        var people = new List<Person>();

        for (int i = 1; i <= numberOfPeople; i++)
        {
            people.Add(
                new Person
                {
                    Id = i,
                    StartTime = startTime.AddTicks((dividedLengthOfTime * i) - dividedLengthOfTime),
                    EndTime = startTime.AddTicks(dividedLengthOfTime * i)
                });
        }
    }

它并没有为我解决整个方程,但我花了两步才找到答案。非常感谢:)
public class Person
{
    public int Id { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}