简体   繁体   English

此方法必须返回int类型的结果

[英]This method must return a result of type int

I attempt to create a function in Java that checks the days in months from 1582 to 2199. Event if i have return statements of int type i get the following error : This method must return a result of type int 我尝试在Java中创建一个函数,该函数检查从1582到2199的月份中的天数。如果我具有int类型的return语句,则会发生以下错误:此方法必须返回int类型的结果

See my sample of code: 请参阅我的代码示例:

/ ------------------------- daysInMonth ---------------------- / / ------------------------- daysInMonth ---------------------- /

public static int daysInMonth(int year, int month)

{

    //returns the number of days in month of year, or -1 if date is invalid.
    //October 1582 has 16 days (16th-31st)

    if (year < 1582 || year > 2199) 

        return -1;


    else if(month == 1 || month ==3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)

         return 31;





}   

There is a combination of parameters when no return is called in your method. 当您的方法中没有调用返回时,参数是组合的。 Javac is smart enough to notice that and it won't compile such code as it can lead to unexpected behavior Javac非常聪明,可以注意到它,并且不会编译此类代码,因为它可能导致意外行为

Event if i have return statements of int type i get the following error : This method must return a result of type int 如果我具有int类型的return语句,则会发生以下错误:此方法必须返回int类型的结果

Yes you do return int, though if the statements all are false. 是的,即使语句全部为假,也可以返回int。 If we do something like daysInMonth(1000, 100); 如果我们做类似daysInMonth(1000, 100);事情daysInMonth(1000, 100); then the program is confused because it ends up at a point where there is nothing to return. 则程序会感到困惑,因为它最终会返回无返回值的位置。

Simply add a else after the else if or just add a return in the end of the method. 只需在else if后面添加else或在方法末尾添加return

public static int daysInMonth(int year, int month)
{
    //returns the number of days in month of year, or -1 if date is invalid.
    //October 1582 has 16 days (16th-31st)

    if (year < 1582 || year > 2199) 
        return -1;
    else if(month == 1 || month ==3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        return 31;

    return -1;
}  

您忘了在else情况下结束if-else语句。

You should also write a case if none of the two conditions meet. 如果两个条件都不满足,您还应该写一个案例。 So better write a case for else after else if . 所以,更好的写的情况下, else以后else if

And what should happen if none of the conditions are satisfied? 如果不满足任何条件怎么办?

I would suggest something like this 我建议这样的事情

int ret = 0;
if (year < 1582 || year > 2199) 
    ret = -1;


else if(month == 1 || month ==3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)

     ret = 31;

return ret;

You should add these lines 您应该添加这些行

else if(month==2)
           {
          if(  year%4==0  )
                 return 29;
             else
                 return 28;
           }


else if( month==4|| month==6|| month==9||                month==11)
  return 30;

 return -1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM