C#/수업 내용

[C#] Method

JSH1 2021. 8. 24. 15:41

매개변수, 반환값이 없는 메서드

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

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            //메서드 호출
            AttackWolf();
        }

        //클래스 내부에 메서드 정의
        //기능: 늑대를 공격합니다.
        //이름 :AttackWolf
        //정의
        static void AttackWolf()
        {
            Console.WriteLine("늑대를 공격했습니다.");
        }
    }
}


매개변수가 있는 메서드

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

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            string weapon = "목도";
            EquipWeapon(weapon);
        }

        static void EquipWeapon(string weaponName)
        {
            Console.WriteLine("{0}를 장착합니다.", weaponName);
        }
    }
}


반환값이 있는 메서드

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

namespace hello_world
{
    class Program
    {
        static int MAX = 3;
        static int mineCount = 3;

        static void Main(string[] args)
        {
            while (true)
            {
                if (InstallMine() == false)
                {
                    break;
                }
            }
        }

        static bool InstallMine()
        {
            if (mineCount > 0)
            {
                mineCount--;
                Console.WriteLine("마인을 설치했습니다. {0}/{1}", mineCount, MAX);
                return true;
            }

            else
            {
                Console.WriteLine("마인이 없습니다.");
                return false;
            }
        }

    }
}


매개변수와 반환값이 있는 메서드

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

namespace hello_world
{
    class Program
    {
        static int MAX = 3;
        static int mineCount = 3;

        static void Main(string[] args)
        {
            Console.WriteLine(FusionTemplars("하이템플러", "하이템플러"));
            Console.WriteLine(FusionTemplars("다크템플러", "다크템플러"));
            Console.WriteLine(FusionTemplars("질럿", "드라군"));
        }

        static string FusionTemplars(string unitA, string unitB)
        {
            if (unitA == "하이템플러" && unitB == "하이템플러")
            {
                return "아콘";
            }

            else if (unitA == "다크템플러" && unitB == "다크템플러")
            {
                return "다크아콘";
            }

            else
            {
                return null;
            }
        }

    }
}

 


 

'C# > 수업 내용' 카테고리의 다른 글

[C#] class  (0) 2021.08.25
[C#] 2021-08-24 오후  (0) 2021.08.24
[C#] 2021-08-24 오전  (0) 2021.08.24
[C#] 2021-08-23 오전  (0) 2021.08.23
[C#] switch case문  (0) 2021.08.23