Several methods are used to find the maximum and minimum values in an integer array.
In an integer array, we need to obtain the maximum and minimum values of array elements:
Method 1: first use Array for sorting, and then from the sorted Array, the most element is the smallest, and the last element is the largest.
Public static int FindMaxNumber (params int [] stringValue) {Array. sort (stringValue); return stringValue [stringValue. length-1];} public static int FindMinNumber (params int [] stringValue) {Array. sort (stringValue); return stringValue [0];}Source Code
Method 2: This method declares a variable whose value is the first element value in the array. The second element starts the loop and compares it with the variable.
Evaluate the maximum element. If the Compare value is smaller than the variable, the element value is assigned to the variable.
The minimum element is the opposite of the maximum value. If the compared value is smaller than the variable, the comparative value is assigned to the variable.
Public static int FindMaxNumber (params int [] intArray) {int v = intArray [0]; if (intArray. length> 1) {for (int I = 2; I <intArray. length; I ++) {if (intArray [I]> v) v = intArray [I];} return v;} public static int FindMinNumber (params int [] intArray) {int v = intArray [0]; if (intArray. length> 1) {for (int I = 2; I <intArray. length; I ++) {if (intArray [I] <v) v = intArray [I] ;}} return v ;}Source Code
Method 3:
Public static int FindMaxNumber (params int [] intArray) {return intArray. Max ();} public static int FindMinNumber (params int [] intArray) {return intArray. Min ();}Source Code
Three methods are demonstrated:
Static void Main (string [] args) {int [] value = {15, 23, 41, 97,100, 27, 67, 51}; Console. writeLine ("Class3:"); Console. writeLine ("Max: {0}", Class3.FindMaxNumber (value); Console. writeLine ("Min: {0}", Class3.FindMinNumber (value); Console. writeLine ("Class4:"); Console. writeLine ("Max: {0}", Class4.FindMaxNumber (value); Console. writeLine ("Min: {0}", Class4.FindMinNumber (value); Console. writeLine ("Class5:"); Console. writeLine ("Max: {0}", Class5.FindMaxNumber (value); Console. writeLine ("Min: {0}", Class5.FindMinNumber (value ));}Source Code
Execution result: