본문 바로가기
c#/수업 내용

반환값이 있는 메서드 연습(짝,홀,오류)

by 이지훈26 2021. 8. 25.
using System;

namespace HelloWorld
{
    class Program
    {
        
        static void Main(string[] args)
        {

            string result = OddorEven(4);
            Console.WriteLine(result);

            Console.WriteLine(OddorEven(5));
            Console.WriteLine(OddorEven(0));

        }
        static string OddorEven(int num)
        {
            if(num == 0)
            {
                return "오류";
            }
            else
            {
                if (num % 2 == 0)
                {
                    return "짝수";
                }
                else
                {
                    return "홀수";
                }
            }
        }
        
    }
}

메인 메서드에서 Console.WriteLine(OddorEven(5));
                      Console.WriteLine(OddorEven(0));  반환형식 호출에 대한 변수를 잘 모르겠었는데, 다시 돌아보고

생각을 해보니 출력의 갯수 만큼 써야한다