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
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;
}
}
+ 1
friend, I thank you