Home  >  Article  >  Java  >  How to find the maximum, minimum, average, sum and other operations of elements in a numerical array in java

How to find the maximum, minimum, average, sum and other operations of elements in a numerical array in java

WBOY
WBOYforward
2023-04-25 10:43:061972browse

Find the maximum value, minimum value, average, sum, etc. of elements in a numerical array

public class T03 {
    public static void main(String[] args) {
        int[] arr=new int[10];
        for(int i=0;i< arr.length;i++){
            arr[i]=(int)Math.random()*((99-10+1)+10);
            //[a,b]中的随机数的公式:Math.readom()*((b-a+1)+a).
            //注意这里出来的为double类型。
        }
        //求最大值
        int maximum=0;
        for(int i=0;i< arr.length;i++){
            if(maximum<arr[i]){
                maximum=arr[i];
            }
        }
        System.out.println("最大值为:"+maximum);
        //求最小值
        int minimum=arr[0];
        for(int i=1;i< arr.length;i++){
            if(minimum>arr[i]){
                minimum=arr[i];
            }
        }
        System.out.println("最大值为:"+minimum);
        //求和
        int sum=0;
        for(int i=1;i< arr.length;i++){
            sum=sum+arr[i];
        }
        System.out.println("sum:"+sum);
        //求平均数
        System.out.println("平均数为:"+sum/ arr.length);
    }
}

The above is the detailed content of How to find the maximum, minimum, average, sum and other operations of elements in a numerical array in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete