Java 我想对数字e的迭代进行编程
我还是Java编程的初学者,我正在使用EclipseNeon。如果不反复编写代码,我怎么能做到这一点呢?最后,我想在屏幕上打印最后的数字 以下是我到目前为止所做的:Java 我想对数字e的迭代进行编程,java,Java,我还是Java编程的初学者,我正在使用EclipseNeon。如果不反复编写代码,我怎么能做到这一点呢?最后,我想在屏幕上打印最后的数字 以下是我到目前为止所做的: public class othermethod { public static void main(String[] args) { int position = 1; int pos = 0; int[] zahlenlaenge = new int[100];
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);
}
}
}
公共类方法{
公共静态void main(字符串[]args){
int位置=1;
int pos=0;
int[]zahlenlaenge=新int[100];
for(int i=0;i
它应该如何实际工作:
好的,在评估了你的要求之后,我认为你的困惑在于你在跟踪你的价值观方面做了什么 我已经用double重新编写了您的大部分代码,并将您的数组重命名为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, ...)
}
double[]foo=新的双精度[100]//将总和存储在数组中的任意点
双和=1d//第一值
双分母=1d//第一分母
for(int i=0;i
您会注意到,随着
1d/分母变得非常小,这会很快正常化,基本上在计算中被忽略。您打算在zahlenlaenge
中存储什么?谷歌翻译说这意味着“数字跟随”,但我不清楚这意味着什么。应该是细分下面的数字,我想让整数=1;然后除以数组,然后像示例中那样进行乘法。注意,不能存储100!在int中,也不能在long中。因此,您不能在zahlenlaenge
数组中存储分母项。您可以用Java编程,但需要使用正确的基元类型来实现。因此,当您编写1时,这是一个整数。根据您使用它的方式,您可能会意外地将预期为双精度的值作为整数返回。为了保持一致性,我希望是双精度的任何东西都标记为1d。我所期望的任何整数都被标记为1。@Javatar排序:除了它显然不是一个类型,而是一个文本。