为什么我的int的值不会改变?

时间:2021-08-30 16:09:12

Hi I'm fairly new to java and I am working on code that works as a calendar. I thought I had completed it but the days seem to remain as 31 rather than changing based on the if/else statement. Here is the code:

嗨,我对java很新,我正在处理作为日历的代码。我以为我已经完成了它但是日子似乎仍然是31而不是基于if / else语句进行更改。这是代码:

public int maxDaysInMonth( int year, int month )
{

boolean A = (year % 4 == 0) || ((year%4==0) && (year % 100 != 0));

int days = 0;
int iMonth = 0;

 if( iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11)
{
    days = 30;
}

else if ( iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7 || iMonth == 8 || iMonth == 10 || iMonth == 12)
{
    days = 31;
}

if (A == true && iMonth == 2)
{ 
    days = 29; 
}
else if (A == false && iMonth == 2)
{
    days = 28;
}

return days;

}

Any help is greatly appreciated!

任何帮助是极大的赞赏!

3 个解决方案

#1


What's the purpose of iMonth? You initialize it to 0 then test it as if it already represented a month.

iMonth的目的是什么?您将其初始化为0然后测试它就像它已经代表一个月。

You don't need iMonth; just use month in your if tests.

你不需要iMonth;只需在if测试中使用月份。

Also, your leap year determination isn't quite correct. It's always a leap year if the year number is divisible by 400. Try

此外,您的闰年确定不太正确。如果年份数可以被400整除,那么它总是闰年。试试

boolean A = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));

You also might want a more descriptive variable name, such as isALeapYear.

您还可能需要更具描述性的变量名称,例如isALeapYear。

#2


Possible Solutions:

1. Replace all iMonth with month - the variable you provided by method parameter.

1.将所有iMonth替换为月份 - 由method参数提供的变量。

2. Call the maxDaysInMonth( int year, int month ) method like this -

2.像这样调用maxDaysInMonth(int year,int month)方法 -

maxDaysInMonth( 2015, 1); // for January  
maxDaysInMonth( 2015, 2); // for February
maxDaysInMonth( 2015, 11); //for December

#3


Like other has mentioned your problem is with iMonth

像其他人一样提到你的问题与iMonth有关

Try using a simple similar code :

尝试使用一个简单的类似代码:

public int maxDaysInMonth(int year, int month) {
    int days;
    switch (month) {
        case 2:
            boolean A = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
            days = A ? 29 : 28;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            days = 30;
            break;
        default:
            days = 31;
    }
    return days;
}

#1


What's the purpose of iMonth? You initialize it to 0 then test it as if it already represented a month.

iMonth的目的是什么?您将其初始化为0然后测试它就像它已经代表一个月。

You don't need iMonth; just use month in your if tests.

你不需要iMonth;只需在if测试中使用月份。

Also, your leap year determination isn't quite correct. It's always a leap year if the year number is divisible by 400. Try

此外,您的闰年确定不太正确。如果年份数可以被400整除,那么它总是闰年。试试

boolean A = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));

You also might want a more descriptive variable name, such as isALeapYear.

您还可能需要更具描述性的变量名称,例如isALeapYear。

#2


Possible Solutions:

1. Replace all iMonth with month - the variable you provided by method parameter.

1.将所有iMonth替换为月份 - 由method参数提供的变量。

2. Call the maxDaysInMonth( int year, int month ) method like this -

2.像这样调用maxDaysInMonth(int year,int month)方法 -

maxDaysInMonth( 2015, 1); // for January  
maxDaysInMonth( 2015, 2); // for February
maxDaysInMonth( 2015, 11); //for December

#3


Like other has mentioned your problem is with iMonth

像其他人一样提到你的问题与iMonth有关

Try using a simple similar code :

尝试使用一个简单的类似代码:

public int maxDaysInMonth(int year, int month) {
    int days;
    switch (month) {
        case 2:
            boolean A = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
            days = A ? 29 : 28;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            days = 30;
            break;
        default:
            days = 31;
    }
    return days;
}