c#獲得數組中最年夜數的值。本站提示廣大學習愛好者:(c#獲得數組中最年夜數的值)文章只能為提供參考,不一定能成為您想要的結果。以下是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;
}
}