- static void Main(string[] args)
- {
- int[] nums = { 1,2,3,4,5,6,};
- int max1, min1, sum1;
- sum1 = compute(nums,out max1,out min1);
- Console.WriteLine("数组的和为{0},最大值{1},最小值{2}",sum1,max1,min1);
- Console.ReadKey();
- }
- /// <summary>
- /// out用于传出值,ref是双向的,既可以传入值,也可以传出值。
- /// </summary>
- /// <param name="number">传入的数组</param>
- /// <param name="max">最大值</param>
- /// <param name="min">最小值</param>
- /// <returns>和</returns>
- static int compute(int[] number,out int max2,out int min2)
- {
- int sum2 = 0;
- max2 = number[0];
- min2 = number[0];
- for (int i = 0; i < number.Length; i++)
- {
- sum2 += number[i];
- if (number[i] > max2)
- {
- max2 = number[i];
- }
- if (number[i] < min2)
- {
- min2 = number[i];
- }
- }
- return sum2;
- }
复制代码 |