ComputerScience/C# 2015. 5. 13. 00:12

스태틱 메소드(static method)

스태틱메소드는 static 킬워드가 붙은 ㅔ소드이다. 같은 클래스 내에서 static 메소드끼리는 

서로 호출할 수 있다. 다른 클래스의 static 메소드를 호풀할 때에는 '클래스이름.스태틱메소드()' 

형태를 취한다.


 메소드 소스

 

 

public static int SumDivision(int n)

{

   int total = 1;

   for(int i=2; i<n; i++)

      {

          if(n%1 == 0)

           {

            total=tatal+1;

           }

      }

      return total;

}

 

 public

어디서든지 사용 할수 있는 접근 제한자 

static 

스태틱 메소드 

int 

메서도의 리턴타입(연산결과)가 int타입 

SumDivision 

메소드 이름 

(int n) 

아규먼트(외부에서 int 타입 데이터를 입력받음) 

{  } 

아규먼트를 받아 연산 

return total 

메소드의 연산 결과를 리턴, 여기서 는 자신을제외한 약수의 총합을 int 타입으로 리턴 




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Com.JungBo.Logic
{
    public class UclidMath
    {
        public static int SumDivision(int n)    // 자신을 제외한 모든 약수의 합
        {
            int total = 1;
            for ( int i = 2; i < n; i++)
            {
                if (n % i == 0) //나누어 떨어지면 약수
                {
                    total = total + i;  //약수의 합
                }
            }
            return total;
        }
        
        public static void printDivision(int n)
        {
            Console.Write("[{0}, ", 1);
            for(int i = 2; i < n; i++)
            {
                if(n % i == 0)  // 나누어 떨어지면
                {
                    Console.Write("{0}, ", i);
                }
            }
            Console.WriteLine("{0}]", n);
        }
        //완전수 인가?

        public static bool IsPerfact(int n)
        {
            bool isP = false;
            if( n == SumDivision(n))
            {
                isP = true;
            }
            return isP;
        }

        public static void PrintPerfact(int n)
        {
            for ( int i = 2; i <= n; i++)
            {
                if(IsPerfact(i))
                {
                    Console.Write("{0}=", i); 
                    printDivision(i);
                }
            }
        }

        
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.JungBo.Logic;

namespace Com.Jungbo.Basic
{
    class Class1
    {
        public static void Main(string[] args)
        {
            int iNum = 10000;
            Console.WriteLine("{0}까지의 완전수 : ", iNum);
            UclidMath.PrintPerfact(iNum);
        }
    }
}

'ComputerScience > C#' 카테고리의 다른 글

break  (0) 2015.05.14
네임스페이스(namespace)  (0) 2015.05.11
Math 클래스  (0) 2015.05.09
등차수열  (0) 2015.05.09
중첩 for문  (0) 2015.05.09