有没有更好的方法来编写这个java代码?还是让它更干净? [英] is there a better way to write this code for java? or to make it cleaner?

查看:96
本文介绍了有没有更好的方法来编写这个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 是什么意思?<$ c是什么$ c> 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;
}

这篇关于有没有更好的方法来编写这个java代码?还是让它更干净?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆