c#求两个数中最大值的方法_C#教程

分享到:

1、三元运算符:


class Program
    {
        static void Main(string[] args)
        {
          int max= NumMAX(10,15);
            Console.WriteLine("最大数:{0}",max);
            Console.ReadKey();
        }
   /// <summary>
        /// 两个数中最大的值
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static int NumMAX(int p1, int p2)
        {
           return p1 > p2 ? p1 : p2;
        }
}

2、if条件判断:


class Program
    {
        static void Main(string[] args)
        {
          int max= NumMAX(10,15);
            Console.WriteLine("最大数:{0}",max);
            Console.ReadKey();
        }
   /// <summary>
        /// 两个数中最大的值
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static int NumMAX(int p1, int p2)
        {
              if (p1>p2)
            {
                return p1;
            }
            return p2;
        }
}

3、Math函数:


class Program
    {
        static void Main(string[] args)
        {
          int max= NumMAX(10,15);
            Console.WriteLine("最大数:{0}",max);
            Console.ReadKey();
        }
   /// <summary>
        /// 两个数中最大的值
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static int NumMAX(int p1, int p2)
        {
           return Math.Max(p1, p2);
        }
}

昵    称:
验证码:

相关文档: