简体   繁体   English

如何安排预建模板的列表? (安慰)

[英]How do I arrange a list to a pre-built templates? (Console)

I have a generic list of names whose number depends on what the user selects. 我有一个通用名称列表,其名称取决于用户选择的名称。 I want to arrange those names in a range of times of the day that the user input before. 我想在用户之前输入的时间范围内排列这些名称。 For example: we have 3 names, the user input 08:00-17:00. 例如:我们有3个名称,用户输入08:00-17:00。 From 08:00-17:00 we have 9 hours -> 9/3=3, so arrange the names in a 3 hours templates. 从08:00-17:00,我们有9个小时-> 9/3 = 3,因此将名称安排在3个小时的模板中。

This is how it looks in code: 它在代码中的外观如下:

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
}

The problem comes, when there are more than 3 names and the hours are halved (lets say 17:30). 问题来了,当名称超过3个并且时间减半时(比如说17:30)。

Any ideas on how to do this kind of thing? 关于如何做这种事情的任何想法?

You could do something like 你可以做类似的事情

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

where Person looks like 人的样子

public class Person
{
    public int Id { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM