Detect months with 31 days

前端 未结 19 1599
南旧
南旧 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:45

    C# as I don't know Java:

    int[] DaysInMonth = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

    if (DaysInMonth[month] == 31) ...

    Forget the fancy logic that many people are advocating--this way is much clearer and easier to debug.

    However, to answer the question you actually asked in your message:

    if (false) ...

    as there are no months with MORE than 31 days!

    Edit: Yes, I didn't address the leap year. That has to be handled separately. The question was whether the month had 31 days, though--something mine DOES answer. I could have done it with an array of bools but since the array needs to be there anyway why not put the lengths in?

    0 讨论(0)
提交回复
热议问题