Detect months with 31 days

前端 未结 19 1661
南旧
南旧 2020-12-18 01:53

Is there an analogous form of the following code:

if(month == 4,6,9,11)
{
  do something;
}

Or must it be:

if(month == 4 ||         


        
19条回答
  •  醉酒成梦
    2020-12-18 02:36

    You don't specify the language, but if you're using Java then yes, you have do do it the second way, or otherwise use switch:

    switch(month) {
      case 4:
      case 6:
      case 9:
      case 11:
        do something;
    }
    

    Alternatively, you might find it useful and cleaner (depending on the design) to not hard-code the values but keep them elsewhere:

    private static final Collection MONTHS_TO_RUN_REPORT = Arrays.asList(4, 6, 9, 11);
    ....
    if (MONTHS_TO_RUN_REPORT.contains(month)) {
      do something;
    }   
    

提交回复
热议问题