zoukankan      html  css  js  c++  java
  • c# 获取数组中最大数的值

    求数组中最大的数的值:
    1、数组的max函数:

     1    class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             int[] array = {1,3,5,2,4,6,7,9,0,8};
     6            int max= GetMax(array);
     7             Console.WriteLine("数组中最大的值是{0}",max);
     8             Console.ReadKey();
     9         }
    10            private static int GetMax(int[] array)
    11          {
    12            return array.Max();
    13          }
    14 }
    View Code

    2、分支语句:

     1  class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             int[] array = {1,3,5,2,4,6,7,9,0,8};
     6            int max= GetMax(array);
     7             Console.WriteLine("数组中最大的值是{0}",max);
     8             Console.ReadKey();
     9         }
    10         /// <summary>
    11         /// 数组中最大的值
    12         /// </summary>
    13         /// <param name="array"></param>
    14         /// <returns></returns>
    15         private static int GetMax(int[] array)
    16         {
    17             int max = 0;
    18             for (int i = 0; i <array.Length; i++)
    19             {
    20                 max = max > array[i] ? max : array[i];
    21 
    22             }
    23             return max;
    24         }
    25     }
    View Code

    3、三元运算:

     1  class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             int[] array = {1,3,5,2,4,6,7,9,0,8};
     6            int max= GetMax(array);
     7             Console.WriteLine("数组中最大的值是{0}",max);
     8             Console.ReadKey();
     9         }
    10         /// <summary>
    11         /// 数组中最大的值
    12         /// </summary>
    13         /// <param name="array"></param>
    14         /// <returns></returns>
    15         private static int GetMax(int[] array)
    16         {
    17             int max = 0;
    18             for (int i = 0; i <array.Length; i++)
    19             {
    20                 max = max > array[i] ? max : array[i];
    21 
    22             }
    23             return max;
    24         }
    25     }
    View Code
  • 相关阅读:
    as3关于正则表达式的那些事
    Flex中使用ArrayCollection的注意事项
    flash资源优化技巧
    测试版权信息
    PHP解密Unicode及Escape加密字符串函数
    php foreach
    硅谷黑暗面:房价过高、政治斗争和岗位侵略
    菜鸟网络股权分配:阿里巴巴占51%的股份
    vue 数据导入加载样式
    CSS文字过多设置省略号
  • 原文地址:https://www.cnblogs.com/zlp520/p/3551960.html
Copyright © 2011-2022 走看看