首页 > 其他分享 >春哥博客 - 方法

春哥博客 - 方法

时间:2023-01-30 09:57:37浏览次数:36  
标签:return 春哥 最大值 博客 int Program 返回值 方法

方法的作用是可以被反复的调用,减少代码冗余

方法的功能一定要单一,一次只完成一件事。

using System;
 
namespace 练习
{
    class Program
    {
        static void Main(string[] args)
        {
           //使用类名.方法名,调用方法
           //使用int类型接收一下返回的最大值,没返回值void的不用接收,有返回值类型的需要接收一下返回值
           //最后输出最大值
           int max =  Program.GetMax(1,3);
            Console.WriteLine(max);
            Console.ReadKey();
        }
        /// <summary>
        /// 使用方法求两个整数的最大值
        /// </summary>
        /// <param name="a">整数1</param>
        /// <param name="b">整数2</param>
        /// <returns>返回最大值</returns>
       
        //返回值类型:int
        //方法名的首字母都大写
        //参数为int类型的两个形参
        public static int GetMax(int a,int b)
        {
            //使用三元表达式比较两个数的最大值
            //然后用return把最大值返回
            return a > b ? a : b;
        }
    }
}

  

方法的作用是可以被反复的调用,减少代码冗余

方法的功能一定要单一,一次只完成一件事。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 using System;   namespace 练习 {     class Program     {         static void Main(string[] args)         {            //使用类名.方法名,调用方法            //使用int类型接收一下返回的最大值,没返回值void的不用接收,有返回值类型的需要接收一下返回值            //最后输出最大值            int max =  Program.GetMax(1,3);             Console.WriteLine(max);             Console.ReadKey();         }         /// <summary>         /// 使用方法求两个整数的最大值         /// </summary>         /// <param name="a">整数1</param>         /// <param name="b">整数2</param>         /// <returns>返回最大值</returns>                 //返回值类型:int         //方法名的首字母都大写         //参数为int类型的两个形参         public static int GetMax(int a,int b)         {             //使用三元表达式比较两个数的最大值             //然后用return把最大值返回             return a > b ? a : b;         }     } }

  

标签:return,春哥,最大值,博客,int,Program,返回值,方法
From: https://www.cnblogs.com/xiaochuncn/p/17074491.html

相关文章