Web-android Engineer first-6-6 programming exercises

Source: Internet
Author: User

Source: http://www.imooc.com/code/1677

Small friends, according to the knowledge, reference comments, in the Code Editor to supplement the code, complete the JAVA program, the output array to achieve the maximum, minimum and average value

Operating effect:

Task

Task Requirements:

1, define an integer array, and assign the initial value 61, 23, 4, 74, 13, 148, 20

2. Define variables to hold the maximum, minimum, sum, and average values, and assume that the first element in the array is both a maximum and a minimum

3. Use the For loop to iterate through the elements in the array, respectively, compared to the assumed maximum and minimum values. Replace the current maximum value if it is larger than the assumed maximum value, or replace the current minimum value if it is smaller than the assumed minimum value

4. The elements in the array are summed up during the loop execution

5. Loop end calculates the average based on the accumulated value and prints out the relevant content

1  Public classHelloWorld {2     3      Public Static voidMain (string[] args) {4         5         //defines an integer array and assigns an initial value6         int[] Nums =New int[] {61, 23, 4, 74, 13, 148, 20 };7         8         intmax = Nums[0];//assume that the maximum value is the first element in the array9         intmin = nums[0];//assume that the minimum value is the first element in the arrayTen         Doublesum = 0;//Accumulated Value One         DoubleAvg = 0;//Average A          -          for(inti = 0; i < nums.length; i++) {//iterate through the elements in an array -         //if the current value is greater than Max, replace the value of Max the          -          -          -         //if the current value is less than min, the value of min is replaced +          -          +          A         //Accumulate sum at          -          -         } -          -         //averaging -         in          -SYSTEM.OUT.PRINTLN ("Maximum value in array:" +max); toSYSTEM.OUT.PRINTLN ("Minimum value in array:" +min); +System.out.println ("average in array:" +avg); -     } the}

1  Public classHelloWorld {2     3      Public Static voidMain (string[] args) {4         5         //defines an integer array and assigns an initial value6         int[] Nums =New int[] {61, 23, 4, 74, 13, 148, 20 };7         8         intmax = Nums[0];//assume that the maximum value is the first element in the array9         intmin = nums[0];//assume that the minimum value is the first element in the arrayTen         Doublesum = 0;//Accumulated Value One         DoubleAvg = 0;//Average A          -          for(inti = 0; i < nums.length; i++) {//iterate through the elements in an array -             //if the current value is greater than Max, replace the value of Max the             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]; A             } at          -         //Accumulate sum -Sum + =Nums[i]; -          -         } -          in         //averaging -      toAVG = SUM/nums.length; +          -SYSTEM.OUT.PRINTLN ("Maximum value in array:" +max); theSYSTEM.OUT.PRINTLN ("Minimum value in array:" +min); *System.out.println ("average in array:" +avg); $     }Panax Notoginseng}

Web-android Engineer first-6-6 programming exercises

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.