VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • C#学习19————方法调用的4个例子

复制代码
#region  输入数字判断是否是数字
#region 方法实现
/*不管是实参或形参,都在类型中开辟了空间的;
    方法的功能一定要单一;
    如GetMax(int n1,int n2); 
    方法中最忌讳的就是提示用户输入的字眼。
///PS:最新版的没有namespace这些,目前我想到的调用方法和类就是自己写上



*/
#endregion

/*代码实现
namespace _方法实现
{


    class Group
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("请输入一个数字:");
            string num = Console.ReadLine();
            int res = GetNum(num);
            Console.WriteLine(res);
        }
        /// <summary>
        /// 这个方法需要判断用户的输入是否是数字
        /// 如果是数字,则返回;
        /// 如果不是数字,则提示用户重新输入;
        /// </summary>
        /// <param name="num"></param>
        public static int GetNum(string num)
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(num);
                    return number;
                }
                catch
                {
                    Console.WriteLine("请重新输入");
                    num= Console.ReadLine();
                }
            }
        }
    }
}
*/
#endregion
#region 输入y或n,输入y结束,输入n继续请求输入
/*
// 输入y或n,输入y结束,输入n继续请求输入
namespace _yn
{
    class Group
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("你是个天才吗?");
            string s=Console.ReadLine();
            YesOrNot(s);
        }
        /// <summary>
        /// 输入y或n,输入y结束,输入n继续请求输入
        /// </summary>
        /// <param name="s"></param>
        public static void YesOrNot(string s)
        {
            bool b = true;
            while (b)
            {
                if (s == "y")
                {
                    Console.WriteLine("你真是个天才!");
                    b = false;
                }
                else if(s == "n")
                {
                    Console.WriteLine("你是个天才吗?");
                    s= Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("你在说什么鬼话,你是天才吗?");
                    s= Console.ReadLine();
                }
            }
        }
    }
}
*/
#endregion
#region 查找两个整数中的最大值
/*
namespace _max
{
    class Group
    {
        public static void Main(string[] args)
        {
            int n1 = 10;
            int n2 = 20;
            int res=GetIntMax(n1,n2);
            Console.WriteLine(res);
        }
        /// <summary>
        /// 查找两个整数中的最大值
        /// </summary>
        /// <param name="n1"></param>
        /// <param name="n2"></param>
        /// <returns></returns>
        public static int GetIntMax(int n1,int n2)
        {
            int n=(n1 > n2)? n1: n2;
            return n;
        }
    }
}

*/
#endregion
#region 计算输入数组的和
namespace _sum
{
    class Group
    {
        public static void Main(string[] args)
        {
            int[] values = { 1, 2, 3, 4, 6, 8, 9, 5, 8, 10 };
            int sum=IntSum(values);
            Console.WriteLine(sum);
        }
        /// <summary>
        /// 计算输入数组的和
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public static int IntSum(int[] values)
        {
            int sum = 0;
            for(int i = 0; i < values.Length; i++)
            {
                sum+=values[i];
            }
            return sum;
        }
    }
}






#endregion
复制代码



原文链接:https://www.cnblogs.com/interesters-together/p/16368436.html


相关教程