我想编写数字e的迭代

时间:2016-07-05 20:17:33

标签: java

我还是Java编程的初学者,我正在使用Eclipse Neon。如果不一次又一次地编写代码,我怎么能这样做呢?最后,我想在屏幕上打印最终的数字。

这是我到目前为止所做的:

public class othermethod {
    public static void main(String[] args) {
        int position = 1;
        int pos = 0;
        int[] zahlenlaenge = new int[100];
        for (int i = 0; i < zahlenlaenge.length; i++) {
            zahlenlaenge[pos] = position;
            position++;
            pos++;
            System.out.println(pos);
        }
    }
}

它应该如何运作:

e function

1 个答案:

答案 0 :(得分:0)

好的,在评估了你的要求之后,我认为你的困惑在于你在追踪你的价值观方面做了什么。

我已经占用了你的大部分代码并用双打重做它,并将你的数组重命名为foo以便我打字。我不确定你在追踪什么,但这可以辨别出什么是总和,什么是分母,如果需要的话,你可以很快找出它们的来源。

    double[] foo = new double[100]; //store the sum at any point in array
    double sum = 1d; //first value
    double denominator = 1d; //first denominator
    for (int i = 0; i < foo.length; i++) {
        foo[i] = sum; //set the sum to i (1, 2, 2.5, ...)
        System.out.println(i + ":" foo[i]);
        denominator *= (i + 1); //calculate the next factorial, (1, 2, 3)
        sum += 1d / denominator; //calculate the sum (2, 2.5, ...)
    }

你会注意到,当1d / denominator变得非常微小时,这很快就会正常化,并且在计算时基本上被忽略了。