Double函数没有返回Double值?

时间:2012-12-14 10:38:52

标签: java double return

public class Nitin12assignA6 {

    public static void main(String args[]) throws IOException {
        series ob = new series();
        ob.input();
        ob.findSum();
        ob.display();
    }
}
    class series {

        int x, n;

        double sum;

        series() {
            x = n = 0;
            sum = 0.0f;
        }

        void input() throws IOException {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("\nEnter the value of x = ");
            x = Integer.parseInt(in.readLine());
            System.out.print("Enter the value of n = ");
            n = Integer.parseInt(in.readLine());
        }

        void display() {
            System.out.println("The sum of Series upto " + n + " terms is: " + sum);
            System.out.println();
        }

        long fact(int num) {
            if(num == 1) {
                return 1;
            }
            return num * fact(num - 1);
        }

        int power(int num, int exp) {
            if(exp == 1) {
                return 1;
            }
            return num * power(num, exp - 1);
        }

        double term(int numr, long denom) {
            return(numr / denom);
        }

        void findSum() {
            int u = 2, l = 4;
            sum = 1.0f;
            for(int i = 0; i < n; i++) {
                if(l % 8 == 0) {
                    sum += term(power(x, u), fact(l));
                    // Test start
                    System.out.println("add" + sum + " power " + power(x, u) + " fact " + fact(l) + " x " + x + " u " + u
                            + " l " + l);
                    System.out.println("term " + term(power(x, u), fact(l)));
                    System.out.println("test term " + term(5, 2)); // printing 2.0
                                                                    // instead of 2.5
                    // Test end
                } else {
                    sum -= term(power(x, u), fact(l));
                    // Test start
                    System.out.println("minus" + sum + " power " + power(x, u) + " fact " + fact(l) + " x " + x + " u " + u
                            + " l " + l);
                    System.out.println("term " + term(power(x, u), fact(l)));
                    // Test end
                }
                u += 2;
                l += 4;
            }
        }
    }

//双项(int numr,long denom) // {return(numr / denom);} 此函数未返回double值。请帮助...这个程序是一个系列最多n个项。示例 - 测试术语返回2.0而不是2.5 ...谢谢。

5 个答案:

答案 0 :(得分:4)

你需要在分裂前明确地加注。

double term(int numr, long denom) {
     return ((double)numr / denom);
}

答案 1 :(得分:1)

double的投射是对除法产生的值进行的,所以你可以阅读

double term(int numr,long denom) {
    return (double) (numr/denom);
}

首先评估numr/denom,并且由于int/long类型很长,结果将是2L。然后它被铸造成双倍。如果您希望将其评估为double,请执行

double term(int numr,long denom) {
    return  ((double)numr/denom);
}

答案 2 :(得分:0)

double term(int numr, long denom) {
    return ((double) numr/denom);
}

您的版本有int/long,它将始终返回非浮点数。

见:

int a = 2;
int b = 4;
double c = a/b; // 0.0
double d = ((double) a/b); // 0.5

答案 3 :(得分:0)

double term(int numr, long denom) {
    return (numr / denom);
}

返回值为double,但除法(numr / denom)会创建long,然后转换为双倍。

答案 4 :(得分:0)

这可能有所帮助。

double term(int numr, long denom) {
    return ((double)numr / denom);
}