当前位置: 动力学知识库 > 问答 > 编程问答 >

java - I want to program the iteration of the number e

问题描述:

I'm still a beginner in Java programming, and I'm using Eclipse Neon. How can I make it like that without writing the code again and again? And at the end, I want to print the final number on the screen.

Here's what I did so far:

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);

}

}

}

How it should actually work:

网友答案:

OK, after evaluating what you're asking, I think your confusion lies in what you're doing with tracking your values.

I've taken most of your code and redone it with doubles, and renamed your array to foo for my typing purposes. I'm not sure what you're tracking, but this provides the discerning of what is sum, what is denominator, and if needed, any other values you can pretty quickly figure out where they come from.

    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, ...)
    }

You'll notice that this quickly normalizes as the 1d / denominator becomes super-tiny and is basically ignored for calculations.

分享给朋友:
您可能感兴趣的文章:
随机阅读: