package day08;
import java.util.Scanner;
/**
* @author 思弈禾
* @PROJECT sggjava
* @create 2022-08-31 15:30
* (求数组的平均值)使用下面的方法头编写两个重载的方法,返回数组的平均数
*/
public class Test_7_8 {
public static void main(String[] args) {
System.out.print("请输入10个整数:");
int[] arr1 = new int[10];
Scanner input = new Scanner(System.in);
for (int i = 0; i < arr1.length; i++) {
arr1[i] = input.nextInt();
}
System.out.print("整型数组的平均值是:");
System.out.println(average(arr1));
System.out.print("请输入10个double型数:");
double[] arr2 = new double[10];
for (int i = 0; i < arr2.length; i++) {
arr2[i] = input.nextDouble();
}
System.out.print("Double型数组的平均值是:");
System.out.println(average(arr2));
}
public static double average(int[] array){
double sum=0;
for (int i = 0; i < array.length; i++) {
sum+=array[i];
}
return (sum/array.length);
}
public static double average(double[] array){
double sum=0.0;
for (int i = 0; i < array.length; i++) {
sum+=array[i];
}
return (sum/array.length);
}
}
(求数组的平均值)使用下面的方法头编写两个重载的方法,返回数组的平均数
最新推荐文章于 2024-09-02 22:33:56 发布