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

//double term(int numr,long denom) //{return (numr/denom);} 这个函数没有返回双精度值。请帮助... 这个程序是找到一个系列最多 n 项的总和。示例 - 测试项返回 2.0 而不是 2.5...谢谢。

4

5 回答 5

4

您需要在除法之前显式地转换为加倍。

double term(int numr, long denom) {
     return ((double)numr / denom);
}
于 2012-12-14T10:42:38.820 回答
1

double对除法产生的值进行转换,因此您可以阅读

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

所以numr/denom首先被评估,由于int/long类型很长,结果将是 2L。然后它被铸造成双倍。如果您希望对它进行双重评估,请执行

double term(int numr,long denom) {
    return  ((double)numr/denom);
}
于 2012-12-14T10:42:56.680 回答
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
于 2012-12-14T10:42:34.970 回答
0
double term(int numr, long denom) {
    return (numr / denom);
}

返回值是double,但 devision(numr / denom)创建了一个long然后将其转换为双精度。

于 2012-12-14T10:43:06.583 回答
0

这可能会有所帮助。

double term(int numr, long denom) {
    return ((double)numr / denom);
}
于 2012-12-14T10:50:57.580 回答