欢迎使用CSDN-markdown编辑器

来源:互联网 发布:黑人软件 编辑:程序博客网 时间:2024/06/08 18:12

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
#region 求多个数的最大值
//按Ctrl+.然后回车就能自动生成我们要的方法
int max1 = GetMax(77, 33, 7, 4, 67, 0, 766);
int max2 = GetMaxLst(new List { 77, 33, 7, 4, 67, 0, 766 });
Console.WriteLine(“调用方法一最大值是:{0}”,max1);
Console.WriteLine(“调用方法二最大值是:{0}”, max2);
#endregion
}
//方法是静态类型原因是,main函数直接调用该方法(未实例化类),所以方法只能是静态的
private static int GetMax(params int[] intarr) //params申明可变参数
{
int max=intarr[0];
for (int i = 1; i < intarr.Length; i++)
{
if (intarr[i] > max)
max = intarr[i];
}
return max;
throw new NotImplementedException();
}
private static int GetMaxLst(List intarr) //利用泛型List类型,当作可变参数求值,注意调用写法
{
int max = intarr[0];
for (int i = 1; i < intarr.Count; i++)
{
if (intarr[i] > max)
max = intarr[i];
}
return max;
throw new NotImplementedException();
}
}
}