| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 717 人关注过本帖
标题:程序老是报错 求解释
只看楼主 加入收藏
hongshuai567
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2012-5-29
结帖率:50%
收藏
已结贴  问题点数:5 回复次数:6 
程序老是报错 求解释
/*
 * 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 
2012-09-23 09:49
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:2 
class  math
{
    public int f(int x)
    {
        if(x==1 || x==2) {
                return 1;
            }
        else {
                return f(x-1)+f(x-2);
            }
        }
}
}

My life is brilliant
2012-09-25 22:03
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:0 
你看,没上色的那个花括号是不是多余的?

My life is brilliant
2012-09-25 22:05
mazak111
Rank: 1
等 级:新手上路
帖 子:19
专家分:7
注 册:2012-7-23
收藏
得分:2 
看看是不是有粗心的地方,比如符号什么写错~~
2012-09-26 16:37
davidfly
Rank: 2
来 自:内蒙古
等 级:论坛游民
帖 子:20
专家分:21
注 册:2011-7-4
收藏
得分:2 
呵呵 把math类放到主类外面去就可以了
也就是:
public class JavaApplication4
{

}
class math
{

}
2012-09-26 18:00
陆嘉文
Rank: 1
来 自:江西
等 级:新手上路
帖 子:96
专家分:6
注 册:2011-11-10
收藏
得分:0 
你把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);
             }
         }

 }
2012-10-05 00:44
despair1989
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-21
收藏
得分:0 
程序代码:
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);
            }
    }
    }
}

你这个内部类调用的不太对,不该是那么调用的,我改了一笑调用方式,这样就可以了。另外,你最好规范一下自己的代码
2012-10-21 21:42
快速回复:程序老是报错 求解释
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022902 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved