C#/수업 내용

1월 1주 복습 (1/2)

Game Client Lee Hwanguk 2023. 1. 7. 20:33

#1열거형식과 상수

1.열거형식과 상수. ItemGrade를 열거형식으로 만들고 안에 NOMAL,MAGIC,LEGEND 상수를 만들자

2.ItemGrade(타입)를 itemGrade(변수)에 할당 

3.할당한 변수를 출력해보자

4.열거형식을 int로 (명시적)변환가능

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

namespace review01
{
    internal class App
    {
        enum ItemGrade
        {
            NORMAL,    //int 형식으로 변환하면 0부터 ~~~
            MAGIC,
            LEGEND

        }
            
        public App()
        {
            ItemGrade itemGrade; //=> Item 열거형식을 item변수로 선언

            //변수에 값 할당 
            itemGrade = ItemGrade.LEGEND;

            //변수의 값 출력 
            Console.WriteLine(itemGrade);

            Console.WriteLine((int)itemGrade); //=>열거형식을 int형식으로 변환 가능 . 그럼 연산도 가능할까?

            
        }
    }
}

#2.타입변환 연습

1.정수 ->문자

2.문자->정수

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

namespace review01
{
    internal class App
    {
        
            
        public App()
        {

            // 타입변환 
            // 정수->문자
            int num = 13;
            Convert.ToString(num);
            Console.WriteLine(num);

            // 문자->정수
            string str = "154";
            Convert.ToInt32(str);
            Console.WriteLine(str);



        }
    }
}

#3.산술연산자

1.+,-,*,/,% (% 값을 나눈 나머지값)

2.전위연산자, 후위연산자

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

namespace review01
{
    internal class App
    {
        
            
        public App()
        {

            //산술 연산자 +,-,*,/,%
            //++ ,-- 전위 연산, 후위 연산 

            //체력 변수 정의 
            int hp;

            //체력 변수에 값 (10) 할당
            hp = 10; //=>한번 타입이 정해진 변수는 다시 지정 안됨

            //변수의 값 출력
            Console.WriteLine(hp);

            //체력 변수의 값을 1만큼 감소
            //hp=hp - 1;  //=>9
            hp=--hp; //=>9 전위연산 ->즉시 실행, 후위연산 ->반대

            //감소된 체력 변수 값 출력
            Console.WriteLine(hp);

            Console.WriteLine(5 % 4);   // output: 1
            Console.WriteLine(5 % -4);  // output: 1
            Console.WriteLine(-5 % 4);  // output: -1
            Console.WriteLine(-5 % -4); // output: -1
        }
    }
}

#4 선택문 if문 (else if, else)

1.if문에서 변수 값이 변하는 순서에 주목

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

namespace review01
{
    internal class App
    {
        
            
        public App()
        {
            ////bool 타입 (논리 데이터형식 true or false)
            ////온오프 , 버튼작동비작동, 장착비장착, .... 등등 사용 할수 있다
            //bool isDie=false;
            //Console.WriteLine(isDie);

            //int hp=0;
            //if (hp==0)
            //{
            //    Console.WriteLine("a");
            //    Console.WriteLine("b");
            //    Console.WriteLine("c");//변수 hp의 값이 0 이라면 a,b,c를 출력해라 
            //}
            //Console.WriteLine("d"); //변수 hp의 값이 0이 아니라면 if문을 실행하지 않는다 d를 출력하게 된다


            ////if문 연습 1
            ////경험치가 100% 이상이면 "레벨업"을 해보자

            //float exp = 84.185f;
            //int level = 1;
            //float getExp = 12.045f;

            //Console.WriteLine("경험치 {0}를 획득했습니다.",getExp);
            //exp=exp + getExp;
            //Console.WriteLine("현제 경험치{0}%", exp);


            //Console.WriteLine("경험치 {0}를 획득했습니다.", getExp);
            //exp = exp + getExp;

            //if (exp>100)
            //{
            //    level=level + 1;
            //    Console.WriteLine("레벨업");
            //    exp = 0;

            //}
            //Console.WriteLine("현제 경험치 {0}",exp);
            //Console.WriteLine("현제 레벨{0}", level);


            //if문 연습2
            //영웅의 체력은 10/10 입니다
            int heroHp = 10;
            int heroMaxHp = 10;
            //늑대의 공격력은 5 입니다 
            int wolf = 5;
            bool heroHit = false;
            //만약 영웅이 늑대에게 공격받는다면 (공격/비공격)
            
            

            if(heroHit==true)
            {
                //늑대에게 공격(5)을 받았습니다
                Console.WriteLine("늑대에게 공격{0}을 받았습니다",wolf);
                heroHp = heroHp - wolf;
            }

            //영웅의 체력은 5/10 입니다 
            Console.WriteLine("{0}/{1}",heroHp,heroMaxHp);
            
            //만약에 영웅이 체력이 0인가?

            if(heroHp==0)
            {
                //영웅의 체력은 0/10 입니다 
                Console.WriteLine("{0}/{1}죽었다",heroHp,heroMaxHp);
            }

            bool heroResurrection = false;

            Console.WriteLine("영웅을 부활시킬까?", heroResurrection);
            if (heroResurrection==true)
            {
                heroHp = heroMaxHp;
                Console.WriteLine("영웅이 부활 했습니다. ");
                Console.WriteLine("영웅의 현재 체력{0}/{1}", heroHp, heroMaxHp);
            }
            //영웅이 부활 했습니다. 
            //영웅의 체력은 10/10 입니다.
        }
    }
}

2.if-else 문 연습 (퍼센트 계산,정수형mosterHp를 float형식으로 변환하고 float per변수에 값을 할당했다 

 float per = (float)monsterHp / monsterMaxHp *100; )

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

namespace review01
{
    internal class App
    {
        
            
        public App()
        {
            //영웅의 이름은 "홍길동" 입니다. 
            string heroName="홍길동";
            //영웅의 공격력은 5 입니다
            int heroDamage = 5;
            Console.WriteLine("영웅 : {0} 공격력: {1}", heroName, heroDamage);
            //고블린이 등장했습니다 (13/13)
            string monsterName = "고블린";
            int monsterHp = 13;
            int monsterMaxHp = monsterHp;
            Console.WriteLine("몬스터 :{0} 체력 : {1}/{2}", monsterName, monsterHp, monsterMaxHp);
            //홍길동이 고블린을 공격(5) 했습니다.
            monsterHp = monsterHp - heroDamage;
            //고블린이 피해(-5)를 받았습니다 

            //고블린 체력 : 8/13
            //고블린의 남은 체력 (%) : 61.53%
            float per = (float)monsterHp / monsterMaxHp *100;
            Console.WriteLine("고블린의 체력은 {0}/{1} {2:0.00}%",monsterHp,monsterMaxHp, per);

            //만약 고블린의 체력이 65% 미만이면 도망/그렇지 않다면 도망가지않음

            if(monsterHp<70)
            {
                Console.WriteLine("{0}가 도망갔다",monsterName);
            }
            else
            {
                Console.WriteLine("{0}는 도망가지 않았다.",monsterName);
            }

        }
    }
}

3.열거형식 값을 if문 안에서 불러옴 ( 열거형식.상수 ... Starcraft.TERRAN)

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

namespace review01
{
    internal class App
    {
        enum Starcraft
        {
            TERRAN,
            ZERG,
            PROTOSS

        }
   
        public App()
        {
            //열거형식으로 스타크래프트 종족 TERRAN,ZERG,PROTOSS를 정의
            //console.readline();을 이용
            //if ,else if, else문을 활용하여 결과값 출력

            Console.WriteLine("please input (0,1,2)");
            int input=Convert.ToInt32(Console.ReadLine());

            if(input==0)
            {
                Console.WriteLine("{0}",Starcraft.TERRAN);
            }
            else if(input==1) 
            {
                Console.WriteLine("{0}", Starcraft.ZERG);

            }
            else if(input==2) 
            {
                Console.WriteLine("{0}", Starcraft.PROTOSS);

            }
            else
            {
                Console.WriteLine("잘못된 입력값");

            }
        }
    }
}