如何对数组列表的元素求和?
How do I sum the elements of an array list?
我正在尝试使用 while 循环来显示某些计算的结果。
循环中的每次迭代都将两个数相除,一个数递增两次,将结果存储在数组列表中,并且应该对数组列表中的所有元素求和。
但是,我的总和结果不正确,我似乎无法弄清楚原因。我应该得到 1956.9272 但它显示 73.3576,知道我做错了什么吗?
这是我的代码:
import java.util.ArrayList;
public class testtest {
public static void main(String[] args) {
// Initialize variables
double num1 = 1;
double num2 = 3;
double sum = 0;
// A new array list is created
ArrayList<Double> myCalc = new ArrayList<Double>();
// While loop is started if num2 is lass than 99
while (num2 <= 99) {
//Divides 1 by num2
double answer = num1 / num2;
// Adds the result to the array list
myCalc.add(answer);
// Twice increments num2, which is the denominator
++num2;
++num2;
// For loop iterates through each element in the array list
for (Double result : myCalc) {
// Sums all the elements in the array list
sum += result;
}
}
// Displays the results of adding the array list elements together
System.out.printf("%.4f", sum);
}
}
您正在做 sum(k = 1 to 49) of 1/(1+2k) * (49+1-k)
。或者在 java:
double sum = 0;
for (int k = 1; k < 50; ++k) {
sum += (50.0 - k)/(1 + 2*k);
}
这确实接近 73。
我只能猜测原始规范也发生了变化 num1
或者更有可能
answer
也必须添加到每个数组元素。
出于调试目的,让我们执行以下操作:
在//初始化变量下添加double ans = 0.0;
在 double answer = num1 / num2;
下添加 ans = ans + answer;
,然后添加 System.out.printf("%.4f %.4f %.4f = %.4f\n", num1, num2, answer, ans);
这将显示 运行 总数和总数的增量。我得到
1.0000 3.0000 0.3333 = 0.3333
1.0000 5.0000 0.2000 = 0.5333
1.0000 7.0000 0.1429 = 0.6762
1.0000 9.0000 0.1111 = 0.7873
...
1.0000 99.0000 0.0101 = 1.9378
所以,也许作业的总数有误,或者您没有包括某些内容。
double n1 = 1, n2 = 3, result, sum = 0;
ArrayList<Double> list = new ArrayList<>();
while( n2 <= 99) {
result = n1/n2;
list.add(result);
n2 += 2;
}
for(double temp: list)
sum = sum + temp;
System.out.println(sum); //1.9377748484749076
如果你在得到的时候把for循环放在里面
double n1 = 1, n2 = 3, result, sum = 0;
ArrayList<Double> list = new ArrayList<>();
while( n2 <= 99) {
result = n1/n2;
list.add(result);
n2 += 2;
for(double temp: list)
sum = sum + temp;
}
System.out.println(sum); //73.35762984798366
我正在尝试使用 while 循环来显示某些计算的结果。
循环中的每次迭代都将两个数相除,一个数递增两次,将结果存储在数组列表中,并且应该对数组列表中的所有元素求和。
但是,我的总和结果不正确,我似乎无法弄清楚原因。我应该得到 1956.9272 但它显示 73.3576,知道我做错了什么吗?
这是我的代码:
import java.util.ArrayList;
public class testtest {
public static void main(String[] args) {
// Initialize variables
double num1 = 1;
double num2 = 3;
double sum = 0;
// A new array list is created
ArrayList<Double> myCalc = new ArrayList<Double>();
// While loop is started if num2 is lass than 99
while (num2 <= 99) {
//Divides 1 by num2
double answer = num1 / num2;
// Adds the result to the array list
myCalc.add(answer);
// Twice increments num2, which is the denominator
++num2;
++num2;
// For loop iterates through each element in the array list
for (Double result : myCalc) {
// Sums all the elements in the array list
sum += result;
}
}
// Displays the results of adding the array list elements together
System.out.printf("%.4f", sum);
}
}
您正在做 sum(k = 1 to 49) of 1/(1+2k) * (49+1-k)
。或者在 java:
double sum = 0;
for (int k = 1; k < 50; ++k) {
sum += (50.0 - k)/(1 + 2*k);
}
这确实接近 73。
我只能猜测原始规范也发生了变化 num1
或者更有可能
answer
也必须添加到每个数组元素。
出于调试目的,让我们执行以下操作:
在//初始化变量下添加double ans = 0.0;
在 double answer = num1 / num2;
ans = ans + answer;
,然后添加 System.out.printf("%.4f %.4f %.4f = %.4f\n", num1, num2, answer, ans);
这将显示 运行 总数和总数的增量。我得到
1.0000 3.0000 0.3333 = 0.3333
1.0000 5.0000 0.2000 = 0.5333
1.0000 7.0000 0.1429 = 0.6762
1.0000 9.0000 0.1111 = 0.7873
...
1.0000 99.0000 0.0101 = 1.9378
所以,也许作业的总数有误,或者您没有包括某些内容。
double n1 = 1, n2 = 3, result, sum = 0;
ArrayList<Double> list = new ArrayList<>();
while( n2 <= 99) {
result = n1/n2;
list.add(result);
n2 += 2;
}
for(double temp: list)
sum = sum + temp;
System.out.println(sum); //1.9377748484749076
如果你在得到的时候把for循环放在里面
double n1 = 1, n2 = 3, result, sum = 0;
ArrayList<Double> list = new ArrayList<>();
while( n2 <= 99) {
result = n1/n2;
list.add(result);
n2 += 2;
for(double temp: list)
sum = sum + temp;
}
System.out.println(sum); //73.35762984798366