函数,在函数中赋值,然后输出

 public int SumZhiShu(int n)
        {
            int sum = 0;

            for (int i = 1; i <= n; i++)
            {
                int count = 0;
                for (int j = 1; j <= i; j++)
                {
                    if (i % j == 0)
                    {
                        count++;
                    }
                }
                if (count == 2)
                {
                    sum += i;
                }
            }

            return sum;
        }

        //输入参数对于函数而言,相当于已经赋值了的变量,直接可用。
        //输出参数相当于定义了一个变量,需要在函数中为其赋值,然后调用函数的时候将所赋值带出函数。
        public void shuchu(int a, out int b)
        {
            b = a + 10;
        }

        static void Main(string[] args)
        {
            HanShu hs = new HanShu();//函数所在类的初始化
            int sum = hs.SumZhiShu(100);//通过类来调用函数
            Console.WriteLine(sum);

            int c = 11;
            int d;
            hs.shuchu(c,out d);

            Console.WriteLine(d);

            Console.ReadLine();

posted @ 2014-12-25 11:01  巽坤  阅读(260)  评论(0编辑  收藏  举报