当前位置: 代码迷 >> Java相关 >> 程序老是报错 求解释
  详细解决方案

程序老是报错 求解释

热度:196   发布时间:2012-09-23 09:49:04.0
程序老是报错 求解释
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication4;

/**
*
* @author lihongshuai
*/
public class JavaApplication4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       int i=0;
        math mymath = new math();
        for(i=1;i<=20;i++) {
            System.out.println(mymath.f(i));
        }


        // TODO code application logic here
    }
class  math
{
    public int f(int x)
    {
        if(x==1 || x==2) {
                return 1;
            }
        else {
                return f(x-1)+f(x-2);
            }
        }
}
}
搜索更多相关的解决方案: public  package  command  void  

----------------解决方案--------------------------------------------------------
class  math
{
    public int f(int x)
    {
        if(x==1 || x==2) {
                return 1;
            }
        else {
                return f(x-1)+f(x-2);
            }
        }
}
}
----------------解决方案--------------------------------------------------------
你看,没上色的那个花括号是不是多余的?
----------------解决方案--------------------------------------------------------
看看是不是有粗心的地方,比如符号什么写错~~
----------------解决方案--------------------------------------------------------
呵呵 把math类放到主类外面去就可以了
也就是:
public class JavaApplication4
{

}
class math
{

}
----------------解决方案--------------------------------------------------------
你把math类写在了主类里面去了,把他弄出来就可以了
程序代码:

public class JavaApplication4 {


     public static void main(String[] args) {
        int i=0;
         math mymath = new math();
         for(i=1;i<=20;i++) {
             System.out.println(mymath.f(i));
         }
     }
}
class  math
{   math()
     {}
     public int f(int x)
     {
         if(x==1 || x==2) {
                 return 1;
             }
         else {
                 return f(x-1)+f(x-2);
             }
         }
}

----------------解决方案--------------------------------------------------------
程序代码:

public class Test{

    public static void main(String[] args) {
       int i=0;
    //下面是调用内部类的方法,因为你这个内部类被JavaApplication4这个类包裹着,所以当你生成内部类math对象的时候,需要指

     
//明,否则就会报错。
       Test.MyMath myMath = new Test().new MyMath();
        for(i=1;i<=20;i++) {
            System.out.println(myMath.f(i));
        }
    }
    class  MyMath{
        public int f(int x){
            if(x==1 || x==2) {
                return 1;
            }else{
                   return f(x-1)+f(x-2);
            }
    }
    }
}

你这个内部类调用的不太对,不该是那么调用的,我改了一笑调用方式,这样就可以了。另外,你最好规范一下自己的代码
----------------解决方案--------------------------------------------------------
  相关解决方案