编写一个程序,包含两个重载方法,返回一个数组的平均数,它 们具有如下的方法声明: public static int getAverage(int array[]); public static double get Average(double array[]);
时间: 2024-03-14 08:48:00 浏览: 115
以下是一个Java程序,包含两个重载方法,分别用于计算整型数组和浮点型数组的平均值:
```
public class ArrayAverage {
public static int getAverage(int array[]) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array.length;
}
public static double getAverage(double array[]) {
double sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array.length;
}
public static void main(String[] args) {
int intArray[] = { 2, 4, 6, 8, 10 };
double doubleArray[] = { 1.5, 2.5, 3.5, 4.5, 5.5 };
System.out.println("整型数组的平均值为:" + getAverage(intArray));
System.out.println("浮点型数组的平均值为:" + getAverage(doubleArray));
}
}
```
在上面的程序中,`getAverage`方法被重载了两次,分别用于计算整型数组和浮点型数组的平均值。在`main`方法中,我们定义了一个整型数组`intArray`和一个浮点型数组`doubleArray`,并调用`getAverage`方法计算它们的平均值并输出结果。输出结果如下:
```
整型数组的平均值为:6
浮点型数组的平均值为:3.5
```
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)