forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise11_12.java
37 lines (31 loc) · 1.01 KB
/
Exercise11_12.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package ch_11;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Scanner;
/**
* 11.12 (Sum ArrayList) Write the following method that returns the sum of all numbers in an ArrayList:
* public static double sum(ArrayList<Double> list)
* <p>
* Write a test program that prompts the user to enter 5 numbers, stores them in an
* array list, and displays their sum.
*/
public class Exercise11_12 {
public static void main(String[] args) {
ArrayList<Double> list = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.print("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
list.add(in.nextDouble());
}
System.out.println("Sum = " + sum(list));
in.close();
}
public static double sum(ArrayList<Double> list) {
double sum = 0;
ListIterator<Double> iterator = list.listIterator();
while (iterator.hasNext()) {
sum += iterator.next();
}
return sum;
}
}