Java练习题02
问题:
编程求一个整数数组的最大值、最小值、平均值和所有元素的和。
代码:
public class Page99{
public static void main(String args[]){
int NumArray[]=new int[]{32,94,15,86,1533,-98,143,140,11,39,65};
int Average = 0;
int Sum = 0;
for(int j = 0; j < NumArray.length; j++){
for(int i = j; i < NumArray.length; i++){
if(NumArray[j] >= NumArray[i]){
int t = NumArray[i];
NumArray[i] = NumArray[j];
NumArray[j] = t;
}
}
}
for(int j = 0; j < NumArray.length; j++){
Sum = NumArray[j] + Sum;
}
Average = Sum/(NumArray.length);
System.out.print("最小值:");
System.out.println(NumArray[0]);
System.out.print("最大值:");
System.out.println(NumArray[NumArray.length-1]);
System.out.print("总 和:");
System.out.println(Sum);
System.out.print("平均值:");
System.out.print(Average);
}
};