如何简化或减少C#中的代码?

时间:2017-04-20 01:42:47

标签: c#

我正在用C#开发一个Windows窗体应用程序,我是这个哈哈的新手。 所以,我有这个代码,它运行良好,但它是很多代码:/,我想简单地或减少... 这是我的代码,是关于小时的比较,例如:

开始日期:20:30

结束日期:21:00

现在日期:20:33。

所以结果会是真的,但我需要做很多比较,我有很多ifs,我怎么能减少呢?...

public int verifyHour()
        {
            string hourMinute = DateTime.Now.ToString("HH:mm:ss");
            bool h1 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(20, 25, 0), new TimeSpan(20, 30, 0));
            if(h1 == true)
            {
                return 1;
            }
            bool h2 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(8, 00, 0), new TimeSpan(9, 00, 0));
            if (h2 == true)
            {
                return 2;
            }
            bool h3 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(9, 00, 0), new TimeSpan(10, 00, 0));
            if (h3 == true)
            {
                return 3;
            }

            bool h4 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(11, 00, 0), new TimeSpan(12, 00, 0));
            if (h4 == true)
            {
                return 4;
            }

            bool h5 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(12, 00, 0), new TimeSpan(13, 00, 0));
            if (h5 == true)
            {
                return 5;
            }
            bool h6 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(13, 00, 0), new TimeSpan(14, 00, 0));
            if (h6 == true)
            {
                return 6;
            }
            bool h7 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(14, 00, 0), new TimeSpan(15, 00, 0));
            if (h7 == true)
            {
                return 7;
            }
            bool h8 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(15, 00, 0), new TimeSpan(16, 00, 0));
            if (h8 == true)
            {
                return 8;
            }
            bool h9 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(16, 00, 0), new TimeSpan(17, 00, 0));
            if (h9 == true)
            {
                return 9;
            }
            bool h10 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(16, 00, 0), new TimeSpan(17, 00, 0));
            if (h10 == true)
            {
                return 10;
            }
            bool h11 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(17, 00, 0), new TimeSpan(18, 00, 0));
            if (h11 == true)
            {
                return 11;
            }
            bool h12 = IsTimeOfDayBetween(Convert.ToDateTime(hourMinute), new TimeSpan(18, 00, 0), new TimeSpan(19, 00, 0));
            if (h12 == true)
            {
                return 12;
            }
            else
            {
                return -1;
            }
        }

5 个答案:

答案 0 :(得分:1)

这是你写的内容的简化,虽然它可能是错的,因为你的h4不会在10:00~11:00之间检查(并继续)并且你在16:00~17:00进行了双重检查,这使你永远不会回来10。

public int verifyHour(DateTime datetime)
{
   if(datetime.Hour == 20 && datetime.Minute >= 25 && datetime.Minute <= 30)
   {
       return 1;
   }
   else(datetime.Hour <= 8 && datetime.Hour <= 18)
   {
       return datetime.Hour - 6;
   }
   return -1;
}

如果您想从字符串验证

public int VerifyHour(string hourminute)
{
    DateTime datetime;
    if (DateTime.TryParse(hourminute, out datetime))
    {
        int result = datetime.Hour;
        if(datetime.Hour == 20 && datetime.Minute >= 25 && datetime.Minute <= 30)
        {
             return 1;
        }
        else(datetime.Hour <= 8 && datetime.Hour <= 18)
        {
             return datetime.Hour - 6;
        }
    }
    return -1;
}

答案 1 :(得分:1)

(过度简化)的建议是:

如果您只是将小时与第二小时进行比较,请执行以下操作:

public static int GetSeconds(DateTime dateTime) {
    return (dateTime.Hour * 3600) + (dateTime.Minutes * 60) + dateTime.Seconds;
}

public bool IsBetween(DateTime target, DateTime start, DateTime end) {
    var targetSeconds = GetSeconds(target);
    var startSeconds = GetSeconds(start);
    var endSeconds = GetSeconds(end);

    return startSeconds <= targetSeconds && endSeconds >= targetSeconds;
}

如果您只是比较小时数,请执行以下操作:

public static int GetMinutes(DateTime dateTime) {
    return (dateTime.Hour * 60) + dateTime.Minutes
}

public bool IsBetween(DateTime target, DateTime start, DateTime end) {
    var targetMinutes = GetMinutes(target);
    var startMinutes = GetMinutes(start);
    var endMinutes = GetMinutes(end);

    return startMinutes <= targetMinutes && endMinutes >= targetMinutes ;
}

答案 2 :(得分:1)

我不清楚OP功能的确切目的是什么,但是如果你在中间部分使用一个循环,你可以减少它。请注意,我已编写此函数以准确返回OP函数返回的内容:

public int verifyHour()
{
    Datetime nowConverted = Convert.ToDateTime(DateTime.Now.ToString("HH:mm:ss"));
    if (IsTimeOfDayBetween(nowConverted, new TimeSpan(20, 25, 0), new TimeSpan(20, 30, 0)))
    {
        return 1;
    }
    for(int i = 2, time = 8; i <= 12; ++i, ++time)
    {
        if (IsTimeOfDayBetween(nowConverted, new TimeSpan(time, 00, 0), new TimeSpan(time + 1, 00, 0))
        {
            return i;
        }
        else if (time == 9) //You don't compare between 10 and 11
        {
            ++time;
        }
        else if (time == 16) //You repeat this twice, so 10 will never be returned
        {
            ++i;
        }
    }
    return -1;
}

答案 3 :(得分:0)

如果原始问题是关于如何确定某个时间是否介于两次之间,那么您应该如何编码:

translate(0px,50px);

再次来自string:

translate(0px,0px);

答案 4 :(得分:-1)

我假设您想要返回与当前时间不同的值,因为您将8AM设置为2.在编程中,您似乎已经获得了switch statement。因此,您可以使用switch语句设置这样的代码。

public int verifyHour()
{
    int hour = DateTime.Now.Hour;
    switch(hour) 
    {
        case 8:
            return 1;
            break; // not really necessary because you are returning but you should put these in for practice
       case 9:
            return 2;
            break;
       ... // so on for the rest of your cases
       default:  
            return -1;
            break;
    }
}

一旦你到达那里,你可以通过考虑你正在做什么输入和你返回什么来简单地进行清理,然后简单地在return语句上进行数学运算,将当前时间改为你想要的值。