2010-05-10 161 views
4

我正在练习递归,我看不出为什么这个方法似乎不起作用。 任何想法?基本的递归方法 - 阶乘

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

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

感谢

+3

究竟是什么意思“不工作”?它做什么,你期望它做什么? – 2010-05-10 13:08:13

回答

12

你的代码似乎工作,但你没有做与返回的任何有价值的东西,把方法调用factfact(5)一个System.out.println的内部,看看你会得到什么。

1

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

@Test 
public void testYourFactorialMethod() { 
    assertEquals(120, fact(5)); 
} 
+1

不是'assertEquals()'一个JUnit API方法吗?我猜JUnit尚未在这里启用,所以你应该更明确一些。如果它实际上是一个常规的API方法,_mea culpa_。 – Pops 2010-05-10 13:25:48

+0

是的 - 这是JUnit - 我试图展示的是他的方法有效。如果你阅读我的评论,我不建议他在任何地方插入我的代码。 – andyczerwonka 2010-05-10 16:42:09

-7

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

这是因为如果你写Fibonatcci递归,计算120你需要36年toget结果的好/坏Algorythm如何影响任何项目

昔日经典例子!!!!!!

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

点网4.0有一个新的类型名称BigInteger和你可以用它来制作使用系统更好的功能

; using 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少。所以

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

以上内容来自Vahid Nasiri blog whiche wrote in Persian

+3

谁在这里回答你的问题? – Hendrik 2010-05-10 13:27:49

+6

他不是在做Factorial,而不是Fibonacci? – corsiKa 2010-05-10 13:28:34

+3

-1:虽然递归可能不是编写阶乘方法的最佳方式,但它是介绍递归概念的简单示例。不要对提问者为什么提出问题做出假设。另外,120的计算来自事实(5);这当然不需要36年的时间来处理。 – Pops 2010-05-10 13:28:44

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" 
    } 
} 
+1

+1热爵士乐。 – Pops 2010-05-10 13:30:55

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); 
} 

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

+0

算法没有错,就像7小时前其他3个海报所说的那样。 OP只是没有做任何程序输出。 – 2010-05-10 21:04:06

+0

但他的代码风格令人困惑。两个出口点,一个不明确的别名和不必要的括号。这是我的观点。问题不仅仅是回答。 – 2010-05-11 10:20:08

+0

这两个退货没有错。此外,初学者可能会难以理解条件运算符的使用。 – helpermethod 2010-05-12 23:10:31

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 
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)); 
    } 
}