1, using the method to achieve: There is a string array: {" Malone ", " Michael Jordan ", " Reggie Miller ", " Timdenken ", " Kobe Bryant "}, Please output the longest string.
Static voidMain (string[] args) { string[] STRs = {"Malone","Michael Jordan","Reggie Miller","Timdenken","Kobe Bryant" }; Console.WriteLine ("The longest string is: {0}", Exportstr (STRs)); Console.readkey (); } /// <summary> ///determine the longest element in a string array/// </summary> /// <param name= "STRs" >array of strings to judge</param> /// <returns>returns the longest string</returns> Static stringEXPORTSTR (params string[] STRs) { stringMaxstr =""; for(inti =0; I < STRs. Length; i++) { if(Strs[i]. Length >Maxstr. Length) {Maxstr=Strs[i]; } } returnMaxstr; }
2, using the method to achieve: Please calculate the average of an integer array. {1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10}. Requirement: Calculates the result if there are decimals, then two digits (rounded) after the decimal point are displayed.
Static voidMain (string[] args) { int[] arr = {1,3,5,7, the, -,4,4,6,8,Ten }; //saves the output to 2 decimal places. Console.WriteLine (Math.Round (AVG (arr),2)); Console.readkey (); } /// <summary> ///calculates the average of an integer array/// </summary> /// <param name= "arr" >array of integers to be computed</param> /// <returns>returns the average of a double type</returns> Static DoubleAVG (params int[] arr) { DoubleAVG =0.00; Doublesum =0.00; for(inti =0; I < arr. Length; i++) {sum+=Arr[i]; } avg= SUM/arr. Length; returnavg; }
. NET Study notes----2015-07-08 (Basic review and Exercise 05)