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