c#获取数组中最大数的值
求数组中最大的数的值:
1、数组的max函数:
class Program
{
static void Main(string[] args)
{
int[] array = {1,3,5,2,4,6,7,9,0,8};
int max= GetMax(array);
Console.WriteLine("数组中最大的值是{0}",max);
Console.ReadKey();
}
/// <summary>
/// 数组中最大的值
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
private static int GetMax(int[] array)
{
return array.max();
}
}
2、分支语句:
class Program
{
static void Main(string[] args)
{
int[] array = {1,3,5,2,4,6,7,9,0,8};
int max= GetMax(array);
Console.WriteLine("数组中最大的值是{0}",max);
Console.ReadKey();
}
/// <summary>
/// 数组中最大的值
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
private static int GetMax(int[] array)
{
int max = 0;
for (int i = 0; i <array.Length; i++)
{
max = max > array[i] ? max : array[i];
}
return max;
}
}
3、三元运算:
class Program
{
static void Main(string[] args)
{
int[] array = {1,3,5,2,4,6,7,9,0,8};
int max= GetMax(array);
Console.WriteLine("数组中最大的值是{0}",max);
Console.ReadKey();
}
/// <summary>
/// 数组中最大的值
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
private static int GetMax(int[] array)
{
int max = 0;
for (int i = 0; i <array.Length; i++)
{
max = max > array[i] ? max : array[i];
}
return max;
}
}