public class Test {
    public static void main(String[] args) {
        System.out.println(method(10));
    }    public static int method(int n) {
         if(n == 1 || n == 2) {
              return 1;
         }else if(n > 2) {
              return method(n - 1) + method(n - 2);
         }
    }
}
C:\Documents and Settings\Administrator.MICROSOF-2DF703\桌面>javac Test.java
Test.java:12: 缺少返回语句
    }
    ^
1 错误

解决方案 »

  1.   


    public class Test1 {
    public static void main(String[] args) {
    System.out.println(method(10));
    } public static int method(int n) {
    if (n == 1 || n == 2) {
    return 1;

    else
    {
    return method(n - 1) + method(n - 2);
    }
    }}
      

  2.   

    public static int method(int n) {
            if(n == 1 || n == 2) {
                  return 1;
            }else {
                  return method(n - 1) + method(n - 2);
            }
        } 
    }
      

  3.   

    最后应该加上一个return语句,返回刚刚计算得到的值
      

  4.   

    因为你method()方法需要返回一个int型值,
    而在你的程序:public static int method(int n) { 
            if(n == 1 || n == 2) { 
                  return 1; 
            }else if(n > 2) { 
                  return method(n - 1) + method(n - 2); 
            } 
        }
    中if可以返回一个,else if可以返回一个,后面默认会有个else,而这个else没有返回值,改成楼上的就可以了。不要用else if
      

  5.   

    public class Test1 { 
        public static void main(String[] args) { 
        
            System.out.println(method(10)); 
        }    public static int method(int n) 
        { 
            if(n == 1 || n == 2) return 1; 
            else   return method(n - 1) + method(n - 2); 
           
        } 

    必须保证有返回值,if语句不能保证有返回值