6-8 编程练习题答案

时间:2023-02-22 14:25:29
import java.util.Scanner;

public class Test
{
	public static void main(String[] args)
	{
		System.out.print("Enter ten double numbers: ");
		Scanner input = new Scanner(System.in);
		double[] array = new double[10];
		for(int i = 0; i < 10; i++)
			array[i] = input.nextDouble();
		System.out.println("The average of the array is " + average(array));
	}

	public static int average(int[] array)
	{
		int sum = 0;
		int average;
		for(int i = 0; i < array.length; i++)
			sum += array[i];
		average = sum / array.length;
		return average;
	}

	public static double average(double[] array)
	{
		double sum = 0.0;
		double average;
		for(int i =0; i < array.length; i++)
			sum += array[i];
		average = sum / array.length;
		return average;
	}
}