Unity3D/C#

C# 강좌 Day-09 (if문)

일등하이 2021. 8. 15. 02:02
반응형

문 (Statement)

 

if : condition 값이 true면 if 블럭을 실행 

if (condition) 
{
  // block of code to be executed if the condition is True
}
int heroHp = 10;
string heroName = "홍길동";
heroHp -= 100;
Console.WriteLine("name: {0}, hp: {1}", heroName, heroHp);

if (heroHp <= 0) {
	Console.WriteLine("{0}님이 사망했습니다.", heroName);
	heroHp = 0;
	Console.WriteLine("name: {0}, hp: {1}", heroName, heroHp);
}

 

 

 

if-else : condition값이 true면 if 블럭을 실행하고 false일경우 else 블럭을 실행 

if (condition)
{
  // block of code to be executed if the condition is True
} 
else 
{
  // block of code to be executed if the condition is False
}
int reinforcePercent = 13;
Random rand = new Random();
int num = rand.Next(0, 101);
if (num > reinforcePercent)
{
	//강화 성공 
}
else 
{ 
	//강화 실패 
}

 

 

 

if-else if : condition1 값이 true면 if블럭 실행, flase고 condition2가 true면 else if 블럭 실행, condition1도 condition2도 false면 else 블럭 실행 

if (condition1)
{
  // block of code to be executed if condition1 is True
} 
else if (condition2) 
{
  // block of code to be executed if the condition1 is false and condition2 is True
} 
else
{
  // block of code to be executed if the condition1 is false and condition2 is False
}
int time = 22;
if (time < 10) 
{
  Console.WriteLine("Good morning.");
} 
else if (time < 20) 
{
  Console.WriteLine("Good day.");
} 
else 
{
  Console.WriteLine("Good evening.");
}
// Outputs "Good evening."

 

 

연습문제 

두 정수 A와 B가 주어졌을 때, A와 B를 비교하는 프로그램을 작성하시오.

첫째 줄에 A가 주어진다. 
두번째 줄에 B가 주어진다. 

첫째 줄에 다음 세 가지 중 하나를 출력한다.
A가 B보다 큰 경우에는 '>'를 출력한다.
A가 B보다 작은 경우에는 '<'를 출력한다.
A와 B가 같은 경우에는 '=='를 출력한다.

-10,000 ≤ A, B ≤ 10,000일경우 "잘못된 입력입니다" 라고 출력 하세요.

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());

            if (a >= -10000 && b <= 10000)
            {
                if (a > b)
                {
                    Console.WriteLine(">");
                }
                else if (a < b)
                {
                    Console.WriteLine("<");
                }
                else {
                    Console.WriteLine("==");
                }
            }
            else
            {
                Console.WriteLine("잘못된 입력 입니다.");
            }

        }
    }
}
using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            //안되경우 
            // a > 10000 또는 
            // a < -10000
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("a: {0}, b: {1}", a, b);
            bool failedA = a > 10000 || a < -10000;
            bool failedB = b > 10000 || b < -10000;
            bool failed = failedA || failedB;
            Console.WriteLine("{0} || {1} => {2}", failedA, failedB, failed);
            if (failed)
            {
                Console.WriteLine("잘못된 값입니다.");
            }
            else 
            {
                if (a > b)
                {
                    Console.WriteLine(">");
                }
                else if (a < b)
                {
                    Console.WriteLine("<");
                }
                else 
                {
                    Console.WriteLine("==");
                }
            }
        }
    }
}
반응형