有没有更好的方法来编写这个java代码?还是让它更干净?

有没有更好的方法来编写这个java代码?还是让它更干净?

问题描述:

我将此代码编写为项目的一部分,这似乎不是最有效的。是否有更简洁的方法来编写此方法?

i wrote this code as part of a project and this doesnt seem the most efficient. is there a cleaner way to write this method?

public static int numberMonth(int parseMonth, Boolean leapYear)
        {
            int month = 0;
            if (parseMonth < 1)
            { month = 0;
                if (parseMonth < 2)
                { month =+ 31;
                    if (parseMonth < 3)
                    {
                        if (leapYear)
                        {
                        month =+ 29;
                        }
                        else if(!(leapYear))
                        {
                            month=+28;
                            if (parseMonth < 4)
                            {
                                month =+ 30;
                                if (parseMonth < 5)
                                {


                                    month =+ 31;
                                if (parseMonth < 6)
                                {
                                    month =+ 31;
                                    if (parseMonth < 7)
                                    {
                                        month =+ 30;
                                        if (parseMonth < 8)
                                        {
                                            month =+ 31;
                                            if (parseMonth < 9)
                                            {
                                                month =+ 31;
                                                if (parseMonth < 10)
                                                {
                                                    month =+ 30;
                                                    if (parseMonth < 11)
                                                    {
                                                        month =+ 31;
                                                        if (parseMonth < 12)
                                                        {
                                                            month =+31;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
          }


您的原始代码存在很多问题。它没有返回一个值(你显然打算返回 month ,但编译器不知道。它不会到达你想要它的任何代码还有其他问题,虽然它们不会阻止你的代码工作,但它们会阻止任何人理解它。 parseMonth 是什么意思? leapYear 是什么意思?为什么名为 month 的变量包含远远大于一年中月数的值?没有任何变量评论解释这一点。

Your original code has a lot of problems. It doesn't return a value (you obviously intend to return month, but the compiler doesn't know that. It won't reach any of the code you want it to. There are other issues too, and while they won't keep your code from working, they will keep anybody from understanding it. What does parseMonth mean? What does leapYear mean? Why can a variable called month contain values far greater than the number of months in a year? There aren't any comments to explain any of this.

如果我正在写这个函数,我会写下面的内容(基于AndyMac的代码稍加修改):

If I were writing this function, I would write the following (based on AndyMac's code with some slight modifications):

public static int numberOfDaysBeforeMonth(int monthNumber, boolean leapYear)
{
    //if monthNumber is out of range, return -1
    if(monthNumber< 1 || monthNumber > 12)
         return -1;

    int[] daysPerMonth= {31,28,31,30,31,30,31,31,30,31,30,31};

    int numberOfDays = 0;

    //add up the days in the months preceding the month in question
    for (int month = 1; month < monthNumber; month++)
       numberOfDays += daysPerMonth[month - 1];

    //add an extra day if it was a leap year and the month is after February
    if (leapYear && monthNumber > 2)
        numberOfDays++;

    return numberOfDays;
}