'C#/수업내용'에 해당되는 글 9건

  1. 2019.04.10 <Overload, Override>, 싱글턴:데이터매니져
  2. 2019.04.03 <n+1번째 인벤토리> Json과 파일읽기.
  3. 2019.04.03 <n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기
  4. 2019.04.01 <인벤토리>Array형으로 롤백
  5. 2019.04.01 <인벤토리>List<T> 제네릭형
  6. 2019.03.29 이따가 분류
  7. 2019.03.28 1차원 배열
  8. 2019.03.27 <foreach in, for, while>

<Overload, Override>, 싱글턴:데이터매니져

C#/수업내용 2019. 4. 10. 00:02


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class Annivia:Champion
    {
        public Annivia()
        {
            this.speed = 3;
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class App
    {
        GameLauncher gameLauncher;
 
        public App()
        {
            this.gameLauncher = new GameLauncher();
        }
 
        public void Start()
        {
            Console.WriteLine("앱 실행");
            this.gameLauncher.Start();
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class BaronNashor:Monster
    {
        public BaronNashor()
        {
            this.speed = 0;
        }
 
        public override void Move(Vector2 movePos)
        {
            Console.WriteLine("바론은 움직일 수 없습니다.");
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class Champion:Character
    {
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class Character
    {
        public CharacterInfo characterInfo;
        public int speed;
 
        public Character()
        {
 
        }
 
        public Character(CharacterInfo characterInfo)
        {
            this.characterInfo = characterInfo;
        }
 
        public virtual void Move(Vector2 movePos)
        {
            var data = DataManager.GetInstance().dicCharacterData[this.characterInfo.id];
 
            float dis = this.characterInfo.pos.GetDistance(this.characterInfo.pos, movePos);
            while (true)
            {
                if (dis > 0)
                {
                    System.Threading.Thread.Sleep(500);
                    var dir = (movePos - this.characterInfo.pos).Normalize();
                    this.characterInfo.pos += dir * speed;
 
                    Console.WriteLine("{0}이(가) ({1:0.00},{2:0.00})로 이동합니다.", data.name, this.characterInfo.pos.x, this.characterInfo.pos.y);
                    dis -= speed;
                }
                else
                {
                    this.characterInfo.pos = movePos;
                    Console.WriteLine("{0}이(가) ({1:0.00},{2:0.00})에 도착했습니다.", data.name, this.characterInfo.pos.x, this.characterInfo.pos.y);
                    break;
                }
            }
        }
 
        public virtual void Attack(Character target)
        {
            var data = DataManager.GetInstance().dicCharacterData[this.characterInfo.id];
            var targetData = DataManager.GetInstance().dicCharacterData[target.characterInfo.id];
 
            //사거리 안에있으면 때림, 사거리 안에 없으면 이동함
            float dis = this.characterInfo.pos.GetDistance(this.characterInfo.pos, target.characterInfo.pos);
            while (true)
            {
                System.Threading.Thread.Sleep(500);
                if (dis > data.range)
                {
                    var dir = (target.characterInfo.pos - this.characterInfo.pos).Normalize();
                    this.characterInfo.pos += dir * speed;
                    //움직임
                    Console.WriteLine("{0}이(가) ({1:0.00},{2:0.00})로 이동합니다.", data.name, this.characterInfo.pos.x, this.characterInfo.pos.y);
 
                    dis -= this.speed;
                }
                else
                {
                    //멈춰서 때림
                    Console.WriteLine("{0}이(가) {1}을(를) 공격합니다.",data.name,targetData.name);
                    //때리면 데미지 들어감
                    target.TakeDamage(data.damage);
                    break;
                }
            }
        }
 
        public virtual void TakeDamage(int damage)
        {
            var data = DataManager.GetInstance().dicCharacterData[this.characterInfo.id];
            this.characterInfo.hp -= damage;
            Console.WriteLine("{0}이(가) {1}의 피해를 입었습니다.\n남은체력:({2}/{3})", data.name, damage,this.characterInfo.hp,data.hp);
        }
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class CharacterData
    {
        public int id;
        public int type;
        public string name;
        public int hp;
        public int damage;
        public float range;
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class CharacterInfo
    {
        public int id;
        public int hp;
        public Vector2 pos;
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
using Newtonsoft.Json;
 
namespace _20190409
{
    public class DataManager
    {
        private static DataManager Instance;
        public Dictionary<int, CharacterData> dicCharacterData;
 
        private DataManager()
        {
            this.dicCharacterData = new Dictionary<int, CharacterData>();
        }
 
        public static DataManager GetInstance()
        {
            if (DataManager.Instance == null)
            {
                DataManager.Instance = new DataManager();
            }
            return DataManager.Instance;
        }
 
        public void LoadData(string path)
        {
            if (File.Exists(path))
            {
                var json = File.ReadAllText(path);
                var characterDatas = JsonConvert.DeserializeObject<CharacterData[]>(json);
                foreach (var data in characterDatas)
                {
                    this.dicCharacterData.Add(data.id, data);
                }
            }
            else
            {
                Console.WriteLine("파일이 음슴.");
            }
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class GameInfo
    {
        //캐릭터 인포(List)
        public List<CharacterInfo> characterInfos;
 
        public GameInfo()
        {
            this.characterInfos = new List<CharacterInfo>();
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
using Newtonsoft.Json;
 
namespace _20190409
{
    public class GameLauncher
    {
        private UserInfo userInfo;
        private Dictionary<int, CharacterData> dicCharacterData = DataManager.GetInstance().dicCharacterData;
        private List<Character> characters;
 
        public GameLauncher()
        {
            this.characters = new List<Character>();
        }
 
        public void Start()
        {
            Console.WriteLine("게임 런쳐 실행");
            this.ReadyToStart();
        }
 
        private void ReadyToStart()
        {
            //게임 시작 준비
            Console.WriteLine("게임 시작을 준비중입니다.");
            //데이터 로드(data)
            DataManager.GetInstance().LoadData("./data/character_data.json");
            //신구 유저 확인(info)
            this.ExistUserInfo("./info");
            //게임 시작
            this.StartGame();
        }
 
        private void StartGame()
        {
            Console.WriteLine("게임이 시작됐습니다.");
            //캐릭터 생성(신규 유저, 기존 유저 구분)
            BaronNashor baron = null;
            Annivia annivia = null;
 
            if (this.userInfo.NewbieCheck == false)
            {
                //기존 유저
                baron = (BaronNashor)this.CreateCharacter<BaronNashor>(this.userInfo.gameInfo.characterInfos[0]);
                annivia = (Annivia)this.CreateCharacter<Annivia>(this.userInfo.gameInfo.characterInfos[1]);
            }
            else
            {
                //신규유저
                baron = this.CreateCharacter<BaronNashor>(0);
                annivia = this.CreateCharacter<Annivia>(1);
 
                baron.characterInfo.pos = new Vector2(1010);
                annivia.characterInfo.pos = new Vector2(11);
            }
            this.characters.Add(baron);
            baron.characterInfo.pos = new Vector2(2020);
            this.characters.Add(annivia);
 
            //게임 진행
            //움직이고
            annivia.Move(new Vector2(1010));
            //움직여서 때리고
            annivia.Attack(baron);
            //원래 자리로 도망
            annivia.Move(new Vector2(11));
            //세이브//종료
            this.SaveUserInfo();
        }
 
        private void ExistUserInfo(string path)
        {
            if (Directory.Exists(path))
            {
                //기존 유저
                Console.WriteLine("기존 유저입니다.");
                //캐릭터 인포 불러오기
                this.userInfo = this.LoadUserInfo("./info/user_info.json");
                this.userInfo.NewbieCheck = false;
            }
            else
            {
                //신규 유저
                Console.WriteLine("신규 유저입니다.");
                //./info폴더 만들기
                Directory.CreateDirectory(path);
                //캐릭터 인포만들기
                this.CreateUserInfo();
                this.userInfo.NewbieCheck = true;
            }
        }
 
        //기존 유저, 유저인포 불러오기
        private UserInfo LoadUserInfo(string path)
        {
            //./info/user_info.json불러오기
            var json = File.ReadAllText(path);
            return JsonConvert.DeserializeObject<UserInfo>(json);
        }
 
        //신규 유저, 유저인포 만들기
        private void CreateUserInfo(UserInfo userInfo = null)
        {
            if (this.userInfo == null)
            {
                if (userInfo == null)
                {
                    this.userInfo = new UserInfo();
                }
                else
                {
                    this.userInfo = userInfo;
                }
            }
        }
 
        //기존 유저, 인포 불러와서, 캐릭터 생성하기
        private T CreateCharacter<T>(CharacterInfo characterInfo) where T : Character, new()
        {
            T character = new T();
            character.characterInfo = characterInfo;
            return character;
        }
 
        //신규유저, 인포 생성해서, 캐릭터 생성하기
        private T CreateCharacter<T>(int id) where T : Character, new()
        {
            var data = dicCharacterData[id];
            var info = new CharacterInfo();
 
            info.id = data.id;
            info.hp = data.hp;
            info.pos = new Vector2(11);
 
            T character = new T();
            character.characterInfo = info;
            return character;
        }
 
        private void SaveUserInfo()//json
        {
            //현재의 유저인포(게임인포(캐릭터인포List)))를 유저인포에 저장
            var characterInfos = new List<CharacterInfo>();
            foreach (var character in this.characters)
            {
                characterInfos.Add(character.characterInfo);
            }
            this.userInfo.gameInfo.characterInfos = characterInfos;
 
            //파일 쓰기
            var json = JsonConvert.SerializeObject(this.userInfo);
            File.WriteAllText("./info/user_info.json", json, Encoding.UTF8);
            Console.WriteLine("저장완료");
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class Monster:Character
    {
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Start();
 
            Console.ReadKey();
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class UserInfo
    {
        //게임인포, 뉴비체크
        public GameInfo gameInfo;
        public bool NewbieCheck;//기존유저일때 false, 신규유저일때 true
 
        public UserInfo()
        {
            this.gameInfo = new GameInfo();
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190409
{
    public class Vector2
    {
        public float x, y;
        
        public Vector2(float x, float y)
        {
            this.x = x;
            this.y = y;
        }
 
        public static Vector2 operator +(Vector2 v1, Vector2 v2)
        {
            return new Vector2(v1.x + v2.x, v1.y + v2.y);
        }
 
        public static Vector2 operator -(Vector2 v1, Vector2 v2)
        {
            return new Vector2(v1.x - v2.x, v1.y - v2.y);
        }
 
        public static Vector2 operator *(Vector2 v1, float Value)
        {
            return new Vector2(v1.x * Value, v1.y * Value);
        }
 
        public static Vector2 operator /(Vector2 v1, float Value)
        {
            return new Vector2(v1.x / Value, v1.y / Value);
        }
 
        public float Dot(Vector2 v1, Vector2 v2)//스칼라 곱
        {
            return v1.x * v2.x + v1.y * v2.y;
        }
 
        public float GetDistance(Vector2 v1, Vector2 v2)
        {
            return (float)(Math.Sqrt(Math.Pow(v1.x - v2.x, 2+ Math.Pow(v1.y - v2.y, 2)));
        }
 
        public Vector2 Normalize()
        {
            var dis = (float)Math.Sqrt(Math.Pow(this.x, 2+ Math.Pow(this.y, 2));
            
            return new Vector2(this.x / dis, this.y / dis);
        }
    }
}
 
cs



ps. 솔바론을 시도하는 애니비아가 있다고? ㄴㅇㄱ 뿌슝빠슝

:

<n+1번째 인벤토리> Json과 파일읽기.

C#/수업내용 2019. 4. 3. 19:00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190403
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Start();
 
            Console.ReadKey();
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190403
{
    public class Item
    {
        public ItemData itemData;
        public ItemInfo itemInfo;
        public Item(ItemInfo itemInfo)
        {
            this.itemInfo = itemInfo;
        }
 
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190403
{
    public class ItemData
    {
        public int id;
        public string name;
        public int max_level;
        public float damage_min;
        public float damage_max;
 
        public ItemData(int id, string name, int max_level, float damage_min, float damage_max)
        {
            this.id = id;
            this.name = name;
            this.max_level = max_level;
            this.damage_min = damage_min;
            this.damage_max = damage_max;
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190403
{
    public class ItemInfo
    {
        public int id;
 
        public int level;
        public float damage;
 
        public ItemInfo(int id,int level,float damage)
        {
            this.id = id;
            this.level = level;
            this.damage = damage;
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
using Newtonsoft.Json;
 
namespace _20190403
{
    public class App
    {
        Dictionary<int, ItemData> dicItems = new Dictionary<int, ItemData>();
        public App()
        {
            var json = File.ReadAllText("weapon_data.json");
            Console.WriteLine(json);
            var arrItemData = JsonConvert.DeserializeObject<ItemData[]>(json);
            foreach (var item in arrItemData)
            {
                dicItems.Add(item.id, item);
            }
        }
        
        public void Start()
        {
            for (int i = 0; i<dicItems.Count;i++)
            {
                var item = CreateItem(i);
                var data = dicItems[item.itemInfo.id];
                Console.WriteLine("{0}, {1}:{2:0.0}~{3:0.0}", item.itemInfo.id, dicItems[i].name, dicItems[i].damage_min, dicItems[i].damage_max);
            }
        }
 
        public Item CreateItem(int id)
        {
            var itemData = dicItems[id];
            var damage = this.GetRandomNumber(itemData.damage_min, itemData.damage_max);
            return new Item(new ItemInfo(itemData.id, 1, (float)damage));
        }
 
        public double GetRandomNumber(double minimum, double maximum)
        {
            var random = new Random();
            return random.NextDouble() * (maximum - minimum) + minimum;
        }
    }
}
 
cs


:

<n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기

C#/수업내용 2019. 4. 3. 10:58


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    class App
    {
        public void Start()
        {
            Console.WriteLine("시작");
 
            string searchItemName = "";
            var inventory = new Inventory();
            inventory.AddItem(new Item("피바라기"));
            inventory.AddItem(new Item("무한의 대검"));
            inventory.AddItem(new Item("피바라기"));
            inventory.DisplayItem();
            Console.WriteLine();
            
            searchItemName = "란두인의 예언";
            Item foundItem = inventory.FindItem("란두인의 예언");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
 
            Console.WriteLine();
            inventory.UpdateItem(ref foundItem, "피바라기");
            Console.WriteLine();
 
            searchItemName = "피바라기";
            foundItem = inventory.FindItem("피바라기");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
            Console.WriteLine();
            
            inventory.UpdateItem(ref foundItem, "워모그의 갑옷");
            Console.WriteLine();
 
            foundItem=inventory.FindItem("무한의 대검");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
            Console.WriteLine();
 
            inventory.RemoveItem("루덴의 메아리");
            Console.WriteLine();
            inventory.RemoveItem("피바라기");
            Console.WriteLine();
 
            inventory.DisplayItem();
        }
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    public class Inventory
    {
        public int inventoryValue = 0;
        public int inventoryMaxValue = 5;
 
        Item[] items = new Item[5];
 
        public Inventory()
        {
        }
 
        public void AddItem(Item item)
        {
            for(int i = 0; i<inventoryMaxValue;i++)
            {
                if(items[i]==null)
                {
                    items[i] = item;
                    inventoryValue++;
                    break;
                }
            }
        }
 
        public void RemoveItem(string name)
        {
            Item foundItem = null;
            int i;
            for (i = 0; i < inventoryMaxValue; i++)
            {
                if (items[i] == null)
                {
 
                }
                else if (items[i].name == name)
                {
                    foundItem = items[i];
                    break;
                }
                i++;
            }
            if (foundItem == null)
            {
                Console.WriteLine("존재하지 않는 아이템은 제거할 수 없습니다.");
            }
            else
            {
                Console.WriteLine("{0}을 제거했습니다.", items[i].name);
                items[i] = null;
                inventoryValue--;
            }
        }
 
        public Item FindItem(string name)
        {
            Item foundItem = null;
            for (int i = 0; i < inventoryMaxValue; i++)
            {
                if (items[i] == null)
                {
 
                }
                else if (items[i].name == name)
                {
                    foundItem = items[i];
                    break;
                }
            }
            if (foundItem == null)
            {
                return null;
            }
            else
            {
                return foundItem;
            }
        }
 
        public void UpdateItem(ref Item item, string name)
        {
            if (item == null)
            {
                Console.WriteLine("업데이트에 실패했습니다.");
            }
            else if(item !=null)
            {
                Console.WriteLine("아이템의 이름을 \"{0}\"에서 \"{1}\"으로 수정했습니다.", item.name, name);
                item.name = name;
            }
        }
 
        public void DisplayItem()
        {
            Item temp;
            for (int i = 0; i < inventoryMaxValue - 1; i++)
            {
                if (items[i] == null)
                {
                    temp = items[i];
                    items[i] = items[i + 1];
                    items[i + 1= temp;
                }
            }
 
            Console.WriteLine("{0}개의 아이템이 있습니다.", inventoryValue);
            int j = 0;
            foreach (var item in items)
            {
                if (items[j] != null)
                {
                    Console.WriteLine("-> {0}", items[j].name);
                    j++;
                }
                else if (items[j] == null)
                {
                    j++;
                }
            }
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    public class Item
    {
        public string name;
 
        public Item(string name)
        {
            this.name = name;
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Start();
 
            Console.ReadKey();
        }
    }
}
 
cs


:

<인벤토리>Array형으로 롤백

C#/수업내용 2019. 4. 1. 19:18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Run();
 
            Console.ReadKey();
        }
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study_06
{
    public class App
    {
        public App()
        {
        }
        public void Run()
        {
            var inventory = new Inventory(5);
            //시작
            while (true)
            {
                Item temp = null;
                for(int i = 0;i<inventory.inventoryVolumeMax-1;i++)
                {
                    if(inventory.items[i]==null)
                    {
                        temp = inventory.items[i];
                        inventory.items[i] = inventory.items[i + 1];
                        inventory.items[i + 1= temp;
                    }
                }
                Console.WriteLine("========================장비가방========================");
                Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/4.가방확장(+5)/0.종료({0}/{1})", inventory.inventoryVolume, inventory.inventoryVolumeMax);
                Console.Write("입력(숫자):");
                var input = Console.ReadLine();
                if (input == "0")//종료
                {
                    Console.WriteLine("인벤토리를 닫습니다.");
                    break;
                }
                else if (input == "1")//아이템 생성
                {
                    if (inventory.inventoryVolume >= inventory.inventoryVolumeMax)
                    {
                        Console.WriteLine("인벤토리가 가득 찼습니다.");
                    }
                    else
                    {
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                        Console.Write("생성할 아이템 이름을 입력:");
                        input = Console.ReadLine();
                        if (input == "롱소드")
                        {
                            inventory.AddItem(new Item(new ItemData(00"롱소드"50)));
                        }
                        else if (input == "숏소드")
                        {
                            inventory.AddItem(new Item(new ItemData(10"숏소드"30)));
                        }
                        else if (input == "빨간포션")
                        {
                            inventory.AddItem(new Item(new ItemData(10"빨간포션"00)));
                        }
                        else if (input == "파란포션")
                        {
                            inventory.AddItem(new Item(new ItemData(10"파란포션"00)));
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else if (input == "2")//아이템 제거
                {
                    if (inventory.inventoryVolume <= 0)
                    {
                        Console.WriteLine("버릴 아이템이 없습니다.");
                    }
                    else
                    {
                        Item founditem = null;
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                        Console.Write("버릴 아이템 이름을 입력:");
                        input = Console.ReadLine();
                        int j = 0;
                        foreach (var item in inventory.items)
                        {
                            if (item.itemData.name == input)
                            {
                                founditem = item;
                                j++;
                                break;
                            }
                        }
                        if (founditem == null)
                        {
                            Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                        }
                        else
                        {
                            Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                            inventory.RemoveItem(founditem,j);
                        }
                    }
                }
                else if (input == "3")//목록보기
                {
                    if (inventory.inventoryVolume <= 0)
                    {
                        Console.WriteLine("인벤토리가 비었습니다.");
                    }
                    else
                    {
                        Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", inventory.inventoryVolume, inventory.inventoryVolumeMax);
                        foreach (Item item in inventory.items)
                        {
                            if(item!=null)
                            {
                                Console.WriteLine(item.itemData.name);
                            }
                        }
                    }
                }
                else if (input == "4")
                {
                    Console.WriteLine("인벤토리 확장완료!");
                    Array.Resize(ref inventory.items, inventory.items.Length + 5);
                    inventory.inventoryVolumeMax += 5;
                }
                else
                {
                    Console.WriteLine("잘못된 입력입니다.");
                }
            }
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Inventory
    {
        public int inventoryVolumeMax;
        public int inventoryVolume;
        public Item[] items;//아이템을 담을 List
 
        //생성자
        public Inventory(int inventoryVolumeMax)
        {
            this.items = new Item[5];
            this.inventoryVolumeMax = inventoryVolumeMax;
        }
 
        //추가
        public void AddItem(Item item)//아이템 추가
        {
            //아이템 추가
            for (int i = 0; i < this.items.Length; i++)
            {
                if (this.items[i] == null)
                {
                    this.items[i] = item;
                    inventoryVolume++;
                    break;
                }
            }
        }
        //제거
        public void RemoveItem(Item founditem, int j)
        {
            foreach(var item in this.items)
            {
                if(item==founditem)
                {
                    this.items[j] = null;
                    inventoryVolume--;
                    break;
                }
            }
        }
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Item
    {
        //아이템 타입들
        public enum eItem
        {
            None = -1,
            Weapon = 0,
            Potion = 1
        }
 
        public ItemData itemData;
 
        //생성자
        public Item(ItemData itemData)
        {
            this.itemData = itemData;
        }
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class ItemData
    {
        //아이템 데이터가 가지고 있어야하는 것, 아이디, 타입, 이름, 공격력, 방어력
        public int id;
        public int type;
        public string name;
        public int damage;
        public int armor;
 
        //아이템 데이터의 생성자
        public ItemData(int id, int type, string name, int damage, int armor)
        {
            this.id = id;
            this.type = type;
            this.name = name;
            this.damage = damage;
            this.armor = armor;
        }
    }
}
cs


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

<n+1번째 인벤토리> Json과 파일읽기.  (0) 2019.04.03
<n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기  (0) 2019.04.03
<인벤토리>List<T> 제네릭형  (0) 2019.04.01
이따가 분류  (0) 2019.03.29
1차원 배열  (0) 2019.03.28
:

<인벤토리>List<T> 제네릭형

C#/수업내용 2019. 4. 1. 18:15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Run();
 
            Console.ReadKey();
        }
    }
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class App
    {
        public App()
        {
 
        }
        public void Run()
        {
            var EquipmentInventory = new Inventory<EquipmentItem>(5);
            var PotionItem = new Inventory<PotionItem>(5);
 
            //시작
            while(true)
            {
                Console.WriteLine("1.장비가방/2.포션가방/0.종료");
                var input = Console.ReadLine();
                if (input == "0")//종료
                {
                    Console.WriteLine("인벤토리를 닫습니다.");
                    break;
                }
                else if (input == "1")
                {
                    while (true)
                    {
                        Console.WriteLine("========================장비가방========================");
                        Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/4.가방확장(+5)/0.종료({0}/{1})", EquipmentInventory.items.Count, EquipmentInventory.inventoryVolumeMax);
                        Console.Write("입력(숫자):");
                        input = Console.ReadLine();
 
                        if (input == "0")//종료
                        {
                            Console.WriteLine("인벤토리를 닫습니다.");
                            break;
                        }
                        else if (input == "1")//아이템 생성
                        {
                            if (EquipmentInventory.items.Count >= EquipmentInventory.inventoryVolumeMax)
                            {
                                Console.WriteLine("인벤토리가 가득 찼습니다.");
                            }
                            else
                            {
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("생성할 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                if (input == "롱소드")
                                {
                                    EquipmentInventory.AddItem(new EquipmentItem(new ItemData(00"롱소드"50)));
                                }
                                else if (input == "숏소드")
                                {
                                    EquipmentInventory.AddItem(new EquipmentItem(new ItemData(10"숏소드"30)));
                                }
                                else if (input == "빨간포션")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "파란포션")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("잘못된 입력입니다.");
                                }
                            }
                        }
                        else if (input == "2")//아이템 제거
                        {
                            if (EquipmentInventory.items.Count <= 0)
                            {
                                Console.WriteLine("버릴 아이템이 없습니다.");
                            }
                            else
                            {
                                EquipmentItem founditem = null;
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("버릴 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                foreach (var item in EquipmentInventory.items)
                                {
                                    if (item.itemData.name == input)
                                    {
                                        founditem = item;
                                        break;
                                    }
                                }
                                if (founditem == null)
                                {
                                    Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                                    EquipmentInventory.RemoveItem(founditem);
                                }
                            }
                        }
                        else if (input == "3")//목록보기
                        {
                            if (EquipmentInventory.items.Count <= 0)
                            {
                                Console.WriteLine("인벤토리가 비었습니다.");
                            }
                            else
                            {
                                Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", EquipmentInventory.items.Count, EquipmentInventory.inventoryVolumeMax);
                                foreach (Item item in EquipmentInventory.items)
                                {
                                    Console.WriteLine(item.itemData.name);
                                }
                            }
                        }
                        else if (input == "4")
                        {
                            Console.WriteLine("인벤토리 확장완료!");
                            EquipmentInventory.inventoryVolumeMax += 5;
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else if (input == "2")
                {
                    while (true)
                    {
                        Console.WriteLine("========================포션가방========================");
                        Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/4.가방확장(+5)/0.종료({0}/{1})", PotionItem.items.Count, PotionItem.inventoryVolumeMax);
                        Console.Write("입력(숫자):");
                        input = Console.ReadLine();
 
                        if (input == "0")//종료
                        {
                            Console.WriteLine("인벤토리를 닫습니다.");
                            break;
                        }
                        else if (input == "1")//아이템 생성
                        {
                            if (PotionItem.items.Count >= PotionItem.inventoryVolumeMax)
                            {
                                Console.WriteLine("인벤토리가 가득 찼습니다.");
                            }
                            else
                            {
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("생성할 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                if (input == "롱소드")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "숏소드")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "빨간포션")
                                {
                                    PotionItem.AddItem(new PotionItem(new ItemData(21"빨간포션"00)));
                                }
                                else if (input == "파란포션")
                                {
                                    PotionItem.AddItem(new PotionItem(new ItemData(31"파란포션"00)));
                                }
                                else
                                {
                                    Console.WriteLine("잘못된 입력입니다.");
                                }
                            }
                        }
                        else if (input == "2")//아이템 제거
                        {
                            if (PotionItem.items.Count <= 0)
                            {
                                Console.WriteLine("버릴 아이템이 없습니다.");
                            }
                            else
                            {
                                PotionItem founditem = null;
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("버릴 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                foreach (var item in PotionItem.items)
                                {
                                    if (item.itemData.name == input)
                                    {
                                        founditem = item;
                                        break;
                                    }
                                }
                                if (founditem == null)
                                {
                                    Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                                    PotionItem.RemoveItem(founditem);
                                }
                            }
                        }
                        else if (input == "3")//목록보기
                        {
                            if (PotionItem.items.Count <= 0)
                            {
                                Console.WriteLine("인벤토리가 비었습니다.");
                            }
                            else
                            {
                                Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", PotionItem.items.Count, PotionItem.inventoryVolumeMax);
                                foreach (Item item in PotionItem.items)
                                {
                                    Console.WriteLine(item.itemData.name);
                                }
                            }
                        }
                        else if (input == "4")
                        {
                            Console.WriteLine("인벤토리 확장완료!");
                            PotionItem.inventoryVolumeMax += 5;
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("잘못된 입력 입니다.");
                }
            }
        }
    }
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Inventory<T>
    {
        public int inventoryVolumeMax;
        public List<T> items;//아이템을 담을 List
 
        //생성자
        public Inventory(int inventoryVolumeMax)
        {
            this.items = new List<T>();
            this.inventoryVolumeMax = inventoryVolumeMax;
        }
 
        //추가
        public void AddItem(T item)//아이템 추가
        {
            //아이템 추가
            this.items.Add(item);
        }
        //제거
        public void RemoveItem(T founditem)
        {
            this.items.Remove(founditem);
        }
    }
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Item
    {
        //아이템 타입들
        public enum eItem
        {
            None = -1,
            Weapon = 0,
            Potion = 1
        }
 
        public ItemData itemData;
 
        //생성자
        public Item(ItemData itemData)
        {
            this.itemData = itemData;
        }
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    class EquipmentItem:Item
    {
        public EquipmentItem(ItemData itemData) : base(itemData)
        {
            if (this.itemData.type != 0)
            {
                Console.WriteLine("해당 아이템은 장비가방에 넣을 수 없습니다.");
            }
            else
            {
                this.itemData = itemData;
                Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
            }
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    class PotionItem:Item
    {
        public PotionItem(ItemData itemData):base(itemData)
        {
            if (this.itemData.type != 1)
            {
                Console.WriteLine("해당 아이템은 포션가방에 넣을 수 없습니다.");
            }
            else
            {
                this.itemData = itemData;
                Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
            }
        }
    }
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class ItemData
    {
        //아이템 데이터가 가지고 있어야하는 것, 아이디, 타입, 이름, 공격력, 방어력
        public int id;
        public int type;
        public string name;
        public int damage;
        public int armor;
 
        //아이템 데이터의 생성자
        public ItemData(int id, int type, string name, int damage, int armor)
        {
            this.id = id;
            this.type = type;
            this.name = name;
            this.damage = damage;
            this.armor = armor;
        }
    }
}
cs


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

<n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기  (0) 2019.04.03
<인벤토리>Array형으로 롤백  (0) 2019.04.01
이따가 분류  (0) 2019.03.29
1차원 배열  (0) 2019.03.28
<foreach in, for, while>  (0) 2019.03.27
:

이따가 분류

C#/수업내용 2019. 3. 29. 15:51


C#에서 중요한 키워드:객체지향, IEnumerable, IEnumerator, delegate, event Colletion, generic

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

<인벤토리>Array형으로 롤백  (0) 2019.04.01
<인벤토리>List<T> 제네릭형  (0) 2019.04.01
1차원 배열  (0) 2019.03.28
<foreach in, for, while>  (0) 2019.03.27
methods  (0) 2019.03.22
:

1차원 배열

C#/수업내용 2019. 3. 28. 11:05

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Study_05

{

    class App

    {

        public App()//생성자

        {

            //초기화

            Console.WriteLine("{0} 기본 생성자 실행"this);//Study_05.App 기본 생성자 실행

        }

 

        public void Run()//앱을 실행시키는 메서드

        {

            Console.WriteLine("App실행");

 

            int[] array1 = new int[5];//단일 배열을 선언함//0, 0, 0, 0, 0

            for (int i = 0; i < array1.Length; i++)

            {

                Console.Write("{0} ", array1[i]);//0, 0, 0, 0, 0

            }

 

            string[] arrFruits = new string[3];//단일 배열을 선언함//null, null, null, null, null

            for (int i = 0; i < array1.Length; i++)

            {

                Console.Write("{0} ", arrFruits[i]);//null, null, null, null, null

            }

            //int[] array2 = new int[5]{ 11, 22, 33, 44, 55 }

            //int[] array3 = { 111,222,333,444,555 };//대체 문법

        }

    }

}

 

Colored by Color Scripter

cs

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

<인벤토리>Array형으로 롤백  (0) 2019.04.01
<인벤토리>List<T> 제네릭형  (0) 2019.04.01
이따가 분류  (0) 2019.03.29
<foreach in, for, while>  (0) 2019.03.27
methods  (0) 2019.03.22
:

<foreach in, for, while>

C#/수업내용 2019. 3. 27. 14:10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_04
{
    class ConsoleApp
    {
        public ConsoleApp()
        {
            Console.WriteLine("Hello World!");
 
            //실습 시작
            string[] arrNames = { "홍길동""임꺽정""홍상직" };
            foreach (string name in arrNames)
            {
                Console.WriteLine("{0}", name);
            }
            Console.WriteLine();
 
            for (int i = 0; i < arrNames.Length; i++)//위의 foreach와 같은 기능
            {
                Console.WriteLine("{0}", arrNames[i]);
            }
            Console.WriteLine();
 
            int j = 0;
            while (true)
            {
                
                Console.WriteLine("{0}", arrNames[j]);
                j++;
                if(j>=arrNames.Length)
                {
                    break;
                }
            }
            Console.WriteLine();
        }
    }
}
 
cs

ps.홍상직은 홍길동의 아버지이다.

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

<인벤토리>Array형으로 롤백  (0) 2019.04.01
<인벤토리>List<T> 제네릭형  (0) 2019.04.01
이따가 분류  (0) 2019.03.29
1차원 배열  (0) 2019.03.28
methods  (0) 2019.03.22
: