C#프로그래밍/복습

복습1

다모아 2023. 7. 23. 23:04

1. HelloWorld

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string name = "홍길동";
            int level = 12;

            Console.WriteLine("이름: {0}", name);
            Console.WriteLine("레벨: {0}", level);
        }
    }
}

2. 결합연산자

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string name = "홍길동";
            float exp = 23.54f;

            string str = "이름 :" + name + "\n경험치 :" + exp + "%";
            Console.WriteLine(str);
        }
    }
}

3. 디아블로 아이템 사전

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string weaponName = "Glass Star";
            string weaponType = "Spirit Stone";
            int minDamage = 30;
            int maxDamage = 35;
            string equipment = "Armor";

            string sumDamage = minDamage + " - " + maxDamage;
            Console.WriteLine("{0}\n{1}\n\n{2}\n{3}", weaponName, weaponType, sumDamage, equipment);
        }
    }
}

 

4. 변환(캐스팅)

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            float floatHeroHp = 10.56f;
            int intHeroHp = (int)floatHeroHp;
            //캐스트 식, 명시적 변환, 캐스팅
            Console.WriteLine(intHeroHp);

            //문자열 (숫자형) -> 숫자 (정수)
            int num = Convert.ToInt32(Convert.ToInt32("123"));
            Console.WriteLine(num);

            //숫자 -> 문자열
            string str = Convert.ToString(num);
            Console.WriteLine(str);

            //정수 -> 실수
            float a = Convert.ToSingle(123);
            Console.WriteLine("exp: {0:0.00}%", a);

            //실수 -> 정수
            int damage = Convert.ToInt32(123.33);
            Console.WriteLine("damage: {0}", damage);

            float aa = (float)23;
            Console.WriteLine(aa);
        }
    }
}

 

5. 영웅 피

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int monsterDamage = 4;
            int maxHeroHp = 15;
            int heroHp = maxHeroHp;
            Console.WriteLine("몬스터에게 공격을 받았습니다.");

            heroHp = heroHp - monsterDamage; // 11

            //영웅의 남은 체력 : 11 (0.73%)
            float hpPercentage = ((float)heroHp / maxHeroHp) * 100;
            Console.WriteLine("영웅의 남은 체력: {0}, ({1}%)", heroHp, (int)hpPercentage);
        }
    }
}

 

6. 스타크래프트

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string teran = "Marine";
            int teranDamage = 6;
            string zerg = "Zergling";
            int zergHp = 35;
            int zergMaxHp = 35;
            Console.WriteLine("{0}이 {1}을 공격 ({2}) 했습니다.", teran, zerg, teranDamage);
            zergHp -= teranDamage;

            string str = String.Format("{0:0.00}", ((float)zergHp / zergMaxHp) * 100);
            Console.WriteLine("{0}이 {1}에게 피해 (-{2})을 받았습니다. ({3}/{4}) {5}%", zerg, teran, teranDamage, zergHp, zergMaxHp, str);
        }
    }
}

 

7. 데이터 타입 (bool, char, object)

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 안죽었다
            bool isDead = false;
            bool isClicked = true;

            char a = 'a';
            Console.WriteLine(a);

            object obj = 1;
            object obj1 = "홍길동";
            object obj2 = '홍';
            object obj3 = true;

            Console.WriteLine("{0}, {1}, {2}, {3}", obj, obj1, obj2, obj3);
        }
    }
}

 

8. var, const

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //var 키워드
            int hp = 10;
            var damage = 12;
            var name = "홍길동";

            //const 키워드
            //상수 정의 할 때 씀
            const float PI = 3.14f;
            const int MaxHp = 10;
        }
    }
}

 

9. 열거형 enum

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

namespace HelloWorld
{
    internal class Program
    {
        enum Season
        {
            Spring,
            Summer,
            Autumn,
            Winter
        }
        static void Main(string[] args)
        {
            //상수 정의
            const int Spring = 1;
            const int Summer = 1;
            const int Autumn = 1;
            const int Winter = 1;

            //상수들의 집합

            //변수
            Season season = Season.Summer;
            Console.WriteLine("season: {0}", season);

            //Season -> int 형식 변환
            Console.WriteLine((int)season);
            int intSeason = (int)season;
            Console.WriteLine(intSeason);
            //int -> Season 형식으로 변환
            // 0 > Spring
            int intSpring = 0;
            Season spring = (Season)intSpring;
            Console.WriteLine("{0}", spring);
        }
    }
}

 

10. enum 활용

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

namespace HelloWorld
{
    internal class Program
    {
        enum eRace
        {
            Terran = 1,
            Protoss = 2,
            Zerg = 3
        }
        static void Main(string[] args)
        {
            Console.Write("번호 입력 :");
            string input = Console.ReadLine();
            Console.WriteLine("입력한 값 : {0}", input);

            eRace race = (eRace)Convert.ToInt32(input);
            Console.WriteLine("{0}작성", race);
        }
    }
}

 

11.  값형식, 참조형식, null값


            //값 형식 : int, float, bool, char, enum ,, 스택 메모리에 값을 저장

            //참조 형식 : string, object ,, 스택 메모리에 값이 저장되어있는 주소를 저장 힙 메모리에 값을 저장
            
            //null 아무것도 참조하지 않는 값

 

12. 산술연산자, 복합할당식

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int i = 123; // 값형식 -> 스택 메모리
            object obj = i; // 힙 메모리에 저장 , 박싱(암시적)

            int ii = (int)obj; //언박싱(명시적)

            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine("Key : {0}", info.Key);
            Console.WriteLine("KeyChar: {0}", info.KeyChar);

            //증가 연산자 ++x, x++
            //감소 연산자 --x, x--

            //나머지 연산자 %
            Console.WriteLine(10 % 2); // 0

            //복합 할당식
            float hp = 10f;
            float damage = 2f;
            float criticalPercent = 1.3f;
            hp = hp - damage;
            damage *= criticalPercent;
            Console.WriteLine("damage: {0}", damage);
        }
    }
}

 

13. 논리 연산자

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

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            bool isSelectTerran = input == "테란";
            Console.WriteLine("테란을 선택했습니까? {0}", isSelectTerran);

            int hp = 10;
            int monsterDamage = 11;
            hp -= monsterDamage;
            bool isDead = hp <= 0;
            Console.WriteLine("isDead : {0}", isDead);
        }
    }
}

'C#프로그래밍 > 복습' 카테고리의 다른 글

JSON 복습2  (0) 2023.07.28
JSON 복습1  (0) 2023.07.28
2차원 배열 복습  (0) 2023.07.26
2048 [완성]  (0) 2023.07.26