如何向其他班级收到阶乘结果?

时间:2017-05-25 16:13:55

标签: java

问题是收到以前计算的结果。

public class FactorialArray {

    public static long[] factorial = new long[21];
    public static int fact = 0;

    static {
        factorial[0] = 1;
    }

    public static long factorial(int x) {
        while (fact < x) {
            long element;
            factorial[fact] = element.getFact(element);
            fact++;
        }
        return factorial[x];
    }

在这里,我计算了阶乘:

public class Factorial {

    private long factorialCalculator(int x){
        if(x==0 || x==1)
            return 1;
        else 
            return  (long)x*factorialCalculator(x-1); 
    }

    public long getFact(int x){
        return factorialCalculator(x);
    }
}

1 个答案:

答案 0 :(得分:0)

public static long [] factorial(int x){         Factorial factObj = new Factorial();

    while (fact < x) {
        long element;
        factorial[fact] = factObj.getFact(fact);
        fact++;
    }
    return factorial;
}

static class Factorial {

    private long factorialCalculator(int x) {
        if (x == 0 || x == 1)
            return 1;
        else
            return (long) x * factorialCalculator(x - 1);
    }

    public long getFact(int x) {
        return factorialCalculator(x);
    }
}