Detect months with 31 days

前端 未结 19 1664
南旧
南旧 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条回答
  •  猫巷女王i
    2020-12-18 02:19

    If you're using C or Java, you can do this:

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

    In some languages, you could even write case 4,6,9,11:. Other possibilities would be to create an array [4,6,9,11], some functional languages should allow something like if month in [4,6,9,11] do something;

    As Lior said, it depends on the language.

    EDIT: By the way, you could also do this (just for fun, bad code because not readable):

    if ((abs(month-5) == 1) || (abs(month-10) == 1)) do_something;
    

提交回复
热议问题