Someone to help me make one so please to save the semester | Sololearn: Learn to code for FREE!
0

Someone to help me make one so please to save the semester

Write a program that requests the loading of numbers by keyboard, get your average. Finish the loading of values ​​when the 0

14th Dec 2017, 1:23 AM
Abraham Esquivel Toral
Abraham Esquivel Toral - avatar
2 Réponses
+ 2
Something like this may work for you: import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class Program { public static void main(String[] args) { List<Double> numsList = new ArrayList<Double>(); Scanner sc = new Scanner(System.in); double input = 0.0; do{ System.out.print("Enter a number: "); input = sc.nextDouble(); numsList.add(input); System.out.println(); } while (Double.compare(input, new Double(0.0)) != 0); double[] nums = numsList.stream().mapToDouble(d -> d).toArray();; System.out.println(average(nums)); } public static double average(double... nums) { double sum = 0.0; for(double num: nums) { sum += num; } return sum; } }
14th Dec 2017, 2:38 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
friend, I thank you
14th Dec 2017, 1:03 PM
Abraham Esquivel Toral
Abraham Esquivel Toral - avatar