1、三元运算符:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int max= NumMAX(10,15); 6 Console.WriteLine("最大数:{0}",max); 7 Console.ReadKey(); 8 } 9 /// <summary> 10 /// 两个数中最大的值 11 /// </summary> 12 /// <param name="p1"></param> 13 /// <param name="p2"></param> 14 /// <returns></returns> 15 private static int NumMAX(int p1, int p2) 16 { 17 return p1 > p2 ? p1 : p2; 18 } 19 }
2、if条件判断:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int max= NumMAX(10,15); 6 Console.WriteLine("最大数:{0}",max); 7 Console.ReadKey(); 8 } 9 /// <summary> 10 /// 两个数中最大的值 11 /// </summary> 12 /// <param name="p1"></param> 13 /// <param name="p2"></param> 14 /// <returns></returns> 15 private static int NumMAX(int p1, int p2) 16 { 17 if (p1>p2) 18 { 19 return p1; 20 } 21 return p2; 22 } 23 }
3、Math函数:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int max= NumMAX(10,15); 6 Console.WriteLine("最大数:{0}",max); 7 Console.ReadKey(); 8 } 9 /// <summary> 10 /// 两个数中最大的值 11 /// </summary> 12 /// <param name="p1"></param> 13 /// <param name="p2"></param> 14 /// <returns></returns> 15 private static int NumMAX(int p1, int p2) 16 { 17 return Math.Max(p1, p2); 18 } 19 }