4

我正在练习递归,但我不明白为什么这种方法似乎不起作用。有任何想法吗?

    public void fact()
    {
        fact(5);
    }

    public int fact(int n)
    {
        if(n == 1){
            return 1;
        }
        return n * (fact(n-1));
    }
}

谢谢

4

8 回答 8

12

您的代码似乎可以工作,但您没有对返回的值做任何事情,将方法调用factfact(5)放在 a 中System.out.println,看看您得到了什么。

于 2010-05-10T13:08:49.817 回答
6

递归部分很好;你只是没有使用它的return值,它被丢弃了。这是您的阶乘代码的完整 Java 应用程序,出于教育目的而略显夸张:

public class Factorial {
    public static String fact(int n) {
        if(n == 1){
            return "1";
        }
        return n + " * " + (fact(n-1)); // what happens if you switch the order?
    }
    public static void main(String[] args) {
        System.out.println(fact(5));
        // prints "5 * 4 * 3 * 2 * 1"
    }
}
于 2010-05-10T13:23:35.920 回答
2

您的代码的简化版本:

public int fact(int n)
{
    if(n == 1){
        return 1;
    }
    return n * (fact(n-1));
}

可能只是:

public int fact(int n)
{
    return n == 1 ? 1 : n * fact(n - 1);
}

但是您的代码没有错,这只是另一种样式(如果您不习惯三元运算符,请保持原样)。在这些情况下,我更喜欢使用三元运算符(请注意代码没有副作用)。

于 2010-05-10T20:53:24.283 回答
1

工作正常。你没有把它分配给任何东西。这是一个可以证明它有效的测试。

@Test
public void testYourFactorialMethod() {
    assertEquals(120, fact(5));
}
于 2010-05-10T13:14:58.547 回答
1
public class Recursive {

    public static void main(String[] argss) {
        System.out.print(fac(3));
    }
    public static int fac(int n) {
        int value = 0;
        if (n == 0) {
            value = 1;
        } else {
            value = n * fac(n - 1);
        }
        return value;
    }
}
// out put 6
于 2014-01-24T08:09:44.820 回答
1

试试这样的事情:(或者直接试试这个)

public class factorial {

    private static int factorial( int n ){
        if (n > 1)  {
            return n * (factorial(n-1));
        } else {
            return 1;
        }
    }

    public static void main(String[] args) {
        System.out.println(factorial(100));
    }
}
于 2016-10-04T19:12:27.893 回答
0
static int factorial(int x) {    
    int result;    
    if (x == 1) {    
        return 1;    
    }    
    // Call the same method with argument x-1    
    result = factorial(x – 1) * x;    
    return result;    
}

有关完整示例,请检查此

http://answersz.com/factorial-program-in-java-using-recursion/

于 2014-12-30T06:26:40.747 回答
-7

用递归方法写斐波那契是完全错误的!!

这是一个关于好/坏算法如何影响任何项目的著名例子

如果你写斐波那契递归,计算120你需要36年才能得到结果!!!!!!

public static int Fibonacci(int x)
{  // bad fibonacci recursive code
if (x <= 1)
      return 1;
return Fibonacci(x - 1) + Fibonacci(x - 2);
}

在 dot net 4.0 中有一个新的类型名称 BigInteger,您可以使用它来制作更好的功能

使用系统;使用 System.Collections.Generic;使用 System.Numerics;//需要一个参考。到这个大会

namespace Fibonaci
{
 public class CFibonacci
 {
   public static int Fibonacci(int x)
   {
       if (x <= 1)
           return 1;
       return Fibonacci(x - 1) + Fibonacci(x - 2);
   }

   public static IEnumerable<BigInteger> BigFib(Int64 toNumber)
   {
       BigInteger previous = 0;
       BigInteger current = 1;

       for (Int64 y = 1; y <= toNumber; y++)
       {
           var auxiliar = current;
           current += previous;
           previous = auxiliar;
           yield return current;
       }
   }
 }
}

你可以像这样使用它

using System;
using System.Linq;

namespace Fibonaci
{
 class Program
 {
   static void Main()
   {
       foreach (var i in CFibonacci.BigFib(10))
       {
           Console.WriteLine("{0}", i);
       }

       var num = 12000;
       var fib = CFibonacci.BigFib(num).Last();
       Console.WriteLine("fib({0})={1}", num, fib);

       Console.WriteLine("Press a key...");
       Console.ReadKey();
   }
 }
}

在这种情况下,您可以计算12000不到一秒钟。所以

使用递归方法并不总是一个好主意

以上代码是从Vahid Nasiri 博客中导入的,该博客是用波斯语写的

于 2010-05-10T13:23:23.057 回答