Answer:
import java.util.Scanner;
public class AverageDemo
{
public static String getNumbers()
{
String numbers;
Scanner scn = new Scanner(System.in);
System.out.println("Enter five to ten numbers all on one line, separated by spaces: ");
numbers = scn.nextLine();
return numbers;
}
public static double calcAverage(String numbers)
{
String[] values = numbers.split(" ");
double total = 0;
for (int i = 0; i < values.length; i++)
{
total += Integer.parseInt(values[i]);
}
if (values.length == 0)
return 0.0;
else
return (total / values.length);
}
// Method definition of printResults: print the results
public static void printResults(String numbers, double average)
{
System.out.printf("\\The average of the numbers %s is %.2f\\", numbers, average);
}
// main method
public static void main(String[] args)
{
// Call the methods
String numbers = getNumbers();
double average = calcAverage(numbers);
printResults(numbers, average);
}
}
Output: