C#/수업 내용

if문, if-else문

hyunjin-dev-log 2021. 12. 23. 17:09
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            //아이디를 입력하세요 : hong
            //비밀번호를 입력하세요 : 123456
            //비밀번호를 다시 입력하세요 : 12345
            //비밀번호가 틀렸습니다.
            //종료

            //아이디를 입력하세요 : hong
            //비밀번호를 입력하세요 :123456
            //비밀번호를 다시 입력하세요 : 123456
            //hong님 환영합니다
            //종료

            Console.Write("아이디를 입력하세요 : ");
            string name = Console.ReadLine();
            Console.Write("비밀번호를 입력하세요 : ");
            int pw1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("비밀번호를 다시 입력하세요 : ");
            int pw2 = Convert.ToInt32(Console.ReadLine());

            if(pw1 != pw2) 
            {
                Console.WriteLine("비밀번호가 틀렸습니다.");
            }
            else
            {
                Console.WriteLine("{0}님 환영합니다.", name);
            }
        }
    }
}

 

 

-----------------------------------------------------------------------------------------------------------------------------------추가연습

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int hp = 6;
            int orcDamage = 5;
            Console.WriteLine("오크에게서 {0}의 피해를 받았습니다.", orcDamage);
            Console.WriteLine("당신의 현재 체력을 기억하십니까?.");
            Console.Write("현재 체력을 정확히 입력해주세요 : ");
            int inputHp = Convert.ToInt32(Console.ReadLine());
            
            if(hp == inputHp) 
            {
                hp -= orcDamage;
                Console.WriteLine("남은 체력 {0}으로 생존.", hp);
            }
            else
            {
                Console.WriteLine("사망.");
            }
        }
    }
}