【发布时间】:2014-06-04 13:34:01
【问题描述】:
下面的代码运行良好,但我想知道对象创建时间是否有任何问题。
import java.util.Scanner;
public class FactorialExample {
public FactorialExample(int n) {
int fact=1;
for(int i=1;i<=n;i++) {
fact=fact*i;
}
System.out.println("the factorial of a given number is::"+fact);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter any Integer Value:");
int value=sc.nextInt();
FactorialExample fe=new FactorialExample(value);
}
}
【问题讨论】:
-
构造函数多用于初始化变量。
-
也许适合这个问题的论坛是codereview.stackexchange.com
-
首先,你的计算会很快溢出int。其次,我推荐一个memoization 优化。最后,“factorail”是“factorial”。
-
这个问题似乎是题外话,因为它是关于代码审查的。可能更适合codereview.stackexchange.com
标签: java