请问用JAVA如何实现一个简单的递归算法?

解决方案 »

  1.   

    求积的递归函数public class factor {
        
        /** Creates a new instance of factor */
        public int Calfactor(int num) {
            int result=1;
            if(num==1) return result;
            else return num*Calfactor(num-1);
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            factor test=new factor();        System.out.println("answer is " + test.Calfactor(6));
        }
        
    }
      

  2.   

    public static void readFolder(String path){
            File io = new File(path);
            if (io.isDirectory()) {
             System.out.println(path + ":");
            File[] file = io.listFiles();
            for (int i = 0; i < file.length; i++) {
             readFolder(file[i].getPath());   //这里递归
            }
            }
            else {
             System.out.println("\t" + io.getPath());
            }
        }该方法显示出给定目录中的文件及子目录中的文件。
      

  3.   

    public class test{
    public static void main(String arg[]){
    int num=0;
    for(i=0;i<100;i++)
    num=num+1;
    System.out.println("value:"+num);
    }
    }
    算个很简单的递归么?
      

  4.   

    int num=0;
    public int a(){
     num++;
     if(num<10){
      a();
     }
    }绝对比楼上的牛.
      

  5.   

    楼上congliu和rower203写的都不错,总之递归方法就是直接或者间接调用自身的方法,最后收敛于基本问题。
      

  6.   

    sunxutx(孤云挂月)写的不是递归, 只是一个一般的循环而已.foolfish(呆鱼)写的是一个extremely simple的递归, 很典型.
      

  7.   

    //一个简单的求数阶乘的算法,代码不全。。
    for(long counter = 0;counter <= 10;counter++)
        factorial(counter);
    public long factorial(long number)
    {
        if (number <= 1)//基本问题都是求0!或1!
            return 1;
        else
            return number*factorial(number - 1);
    }
      

  8.   

    rower203(华仔)的很好,谢谢了!