Home > Java > javaTutorial > 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
Release: 2023-04-25 10:43:06
forward
2011 people have browsed it

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);
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template