C#:如何将开关与if语句组合?

时间:2009-03-02 09:05:47

标签: c# .net switch-statement if-statement

我需要将一个开关与一个if语句结合起来。

我该怎么做?我想做这样的事情:

switch (periodtype)
{
    if(starttime>endtime)
    {
        ;
    }
    else
    {
        case 0: nextRunTime = nextRunTime.AddHours(period); break;
        case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
        case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
    }
}

5 个答案:

答案 0 :(得分:15)

出了什么问题:

if(starttime<=endtime)
{
    switch (periodtype)
    {
        case 0: nextRunTime = nextRunTime.AddHours(period); break;
        case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
        case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
    }
}

答案 1 :(得分:2)

if(starttime>endtime)
{
    // Stuff
}
else
{
    switch (periodtype)
    {
        case 0: nextRunTime = nextRunTime.AddHours(period); break;
        case 1: nextRunTime = nextRunTime.AddMinutes(period); break
        case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
    }
}

喜欢那个?

答案 2 :(得分:1)

if(starttime>endtime)
{
    ;
}
else
{
   switch (periodtype)
   {
      case 0: nextRunTime = nextRunTime.AddHours(period); break;
      case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
      case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
   }
}

答案 3 :(得分:0)

if(starttime <= endtime)
{
      switch (periodtype)
      {     
             case 0: nextRunTime = nextRunTime.AddHours(period); break;
             case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
             case 2: nextRunTime = nextRunTime.AddSeconds(period); break;

       }
}

答案 4 :(得分:0)

回答

太迟了

这不容易:

if (starttime <= endtime)
{
    switch (periodtype)
    {   
        case 0: nextRunTime = nextRunTime.AddHours(period); break;
        case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
        case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
    }
}