C#/수업 내용

[C#] if

JSH1 2021. 8. 19. 11:55
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)
        {
            // if문
            // 만약에 ~ condition값이 true라면 {if 본문}을 실행한다
            // condition값이 false라면 {if 본문}을 실행하지 않는다
            // 주사위를 굴려서 나온값이 짝수이면 ~ 성공
            int dice_num = 4;
            bool is_success = (dice_num % 2) == 0;
            if(is_success)
            {
                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)
        {
            // if문
            // 만약에 ~ condition값이 true라면 {if 본문}을 실행한다
            // condition값이 false라면 {if 본문}을 실행하지 않는다

            //숫자를 입력하세요 (0~100): 4
            //4는 짝수입니다

            //질문을 출력한다
            Console.WriteLine("숫자를 입력하세요 (0~100): ");

            //입력 받는다
            string input = Console.ReadLine();

            //문자열을 숫자로 변환
            int num = Convert.ToInt32(input);

            //변환된 숫자를 2로 % 연산해서 0이면 짝수
            if(num%2==0)
            {
                Console.WriteLine("{0}는 짝수 입니다.", num);
            }
            //변환된 숫자를 2로 % 연산해서 1이면 홀수
            else if(num%2==1)
            {
                Console.WriteLine("{0}는 홀수 입니다.", num);
            }
        }
    }
}

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

2021-08-19 연습문제  (0) 2021.08.19
[C#] else if  (0) 2021.08.19
[C#] 논리 연산자  (0) 2021.08.19
[C#] 산술 연산자  (0) 2021.08.18
[C#] ConsoleKeyInfo, 키 입력  (0) 2021.08.18