Maximum, minimum, neutralization, average in Java arrays

Source: Internet
Author: User

public class Lxjavacx {

public static void Main (string[] args) {

Defines an integer array and assigns an initial value

int[] Nums = new int[] {49, 61, 23, 4, 74,160, 13, 148, 20, 150};

int max = nums[0]; Assume that the maximum value is the first element in the array

int min = nums[0]; Assume that the minimum value is the first element in the array

Double sum = 0;//Cumulative value

Double avg = 0;//Average

for (int i = 0; i < nums.length; i++) {//Iterate through elements in an array

If the current value is greater than Max, replace the value of Max

if (Nums[i]>max) {

max = Nums[i];

}

If the current value is less than min, the value of min is replaced

if (nums[i]<min) {

min = nums[i];

}

Accumulate sum

sum = Sum+nums[i];

}

Averaging

AVG = sum/nums.length;

SYSTEM.OUT.PRINTLN ("Maximum value in the array:" + max);

SYSTEM.OUT.PRINTLN ("Minimum value in the array:" + min);

System.out.println ("Average in the array:" + avg);

}

}


Maximum, minimum, neutralization, average in Java arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.