用java 程序功能描述:对一个数组进行操作,实现输出数组中的最大值、最小值和平均值。
时间: 2024-03-26 14:34:08 浏览: 77
好的,以下是Java程序的解决方案:
```java
public class ArrayOperation {
public static void main(String[] args) {
int[] arr = {10, 5, 7, 2, 15}; // 假设这是要操作的数组
int max = arr[0]; // 假设最大值是数组的第一个元素
int min = arr[0]; // 假设最小值是数组的第一个元素
double sum = 0; // 用于累加数组元素的和
// 遍历数组,找到最大值和最小值,并计算数组元素的和
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
sum += arr[i];
}
double avg = sum / arr.length; // 计算平均值
System.out.println("最大值为:" + max);
System.out.println("最小值为:" + min);
System.out.println("平均值为:" + avg);
}
}
```
程序先定义了一个整型数组,然后用循环遍历数组,找到最大值和最小值,并计算数组元素的和。最后,计算平均值并输出最大值、最小值和平均值。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)