码迷,mamicode.com
首页 > 编程语言 > 详细

调用方法计算数组的和,最大值,最小值

时间:2015-12-18 14:39:10      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication7
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //调用方法计算数组的和,最大值,最小值
14             int[] array = { 5, 2, 1, 6, 9, 11, 18, 3 };
15             int max, min, sum;
16             sum = compute(array,out max,out min);//该行的max,min,sum和上一行的为同一变量,但与调用方法中的同名变量不同,调用方法的变量只是返回值给本行相同名变量
17             Console.WriteLine("数组的和为:{0}    最大值为{1} 最小值为{2}",sum,max,min);
18             Console.ReadKey();
19         }
20 
21         static int compute(int[] numbers, out int max, out int min)
22         {
23             int sum = 0;
24             max = numbers[0];
25             min = numbers[0];
26             for (int i = 0; i < numbers.Length; i++)
27             {
28                 sum += numbers[i];
29                 if (numbers[i] > max)
30                 {
31                     max = numbers[i];
32                 }
33                 if (numbers[i] < min)
34                 {
35                     min = numbers[i];
36                 }
37             }
38             return sum;
39         }
40         
41     }
42 }

 

调用方法计算数组的和,最大值,最小值

标签:

原文地址:http://www.cnblogs.com/start-from-scratch/p/5056934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!