200k views
5 votes
Write a single statement that assigns avg_sales with the average of num_sales1, num_sales2, and num_sales3.

Sample output with inputs: 3 4 8
Average sales: 5.00

User Kyung
by
6.6k points

1 Answer

0 votes

Answer:

import java.io.*;

import java.util.*;

public class Main {

public static void main(String[] args) throws IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String numSales;

ArrayList<Integer> average = new ArrayList<>();

while ((numSales = in.readLine()) != null) {

int number = Integer.parseInt(numSales);

average.add(number);

}

OptionalDouble avg = average.stream().mapToDouble(a -> a).average();

System.out.println(avg);

}

}

Step-by-step explanation:

Just convert the input to an array, and then find the average after looping for all of the values in the input.

From the sample input of:

3

4

8

My output gives:

5.0

User Codneto
by
6.4k points