'C#'에 해당되는 글 38건

  1. 2019.03.31 <인벤토리 1차>중첩방식 목록보기
  2. 2019.03.29 이따가 분류
  3. 2019.03.28 Boxing Unboxing
  4. 2019.03.28 델리게이트
  5. 2019.03.28 1차원 배열
  6. 2019.03.27 <인벤토리> 0.5차
  7. 2019.03.27 IndexOf와 static 한정자
  8. 2019.03.27 <foreach in, for, while>

<인벤토리 1차>중첩방식 목록보기

C#/과제 2019. 3. 31. 18:35



Program.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 InventoryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Run();
 
            Console.ReadKey();
        }
    }
}
 
cs

App.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class App
    {
        public App()
        {
 
        }
        public void Run()
        {
            var inventory = new Inventory(10);//인벤토리 생성, 및 최대량 지정
            Console.WriteLine("인벤토리 오픈");
 
            //메인메뉴
            while (true)
            {
                Console.WriteLine("===================================================================");
                Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/0.종료({0}/{1})", inventory.items.Count, inventory.inventoryVolumeMax);
                Console.Write("입력(숫자):");
                var input = Console.ReadLine();
 
                if (input == "0")//종료
                {
                    Console.WriteLine("인벤토리를 닫습니다.");
                    break;
                }
                else if (input == "1")//아이템 생성
                {
                    if (inventory.items.Count >= 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(21"천옷"05)));
                        }
                        else if (input == "판금갑옷")
                        {
                            inventory.AddItem(new Item(new ItemData(31"판금갑옷"010)));
                        }
                        else if (input == "반지")
                        {
                            inventory.AddItem(new Item(new ItemData(42"반지"01)));
                        }
                        else if (input == "목걸이")
                        {
                            inventory.AddItem(new Item(new ItemData(52"목걸이"00)));
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else if (input == "2")//아이템 제거
                {
                    if (inventory.items.Count <= 0)
                    {
                        Console.WriteLine("버릴 아이템이 없습니다.");
                    }
                    else
                    {
                        Item founditem = null;
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 천옷, 판금갑옷, 반지, 목걸이)");
                        Console.Write("버릴 아이템 이름을 입력:");
                        input = Console.ReadLine();
 
                        foreach (var item in inventory.items)
                        {
                            if (item.itemData.name == input)
                            {
                                founditem = item;
                                break;
                            }
                        }
                        if (founditem == null)
                        {
                            Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                        }
                        else
                        {
                            Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                            inventory.RemoveItem(founditem);
                        }
                    }
                }
                else if (input == "3")//목록보기
                {
                    int countLongSword = 0, countShortSword = 0, countCloth = 0, countPlateArmor = 0, countRing = 0, countNecklace = 0;//아이템 카운터
                    if (inventory.items.Count <= 0)
                    {
                        Console.WriteLine("인벤토리가 비었습니다.");
                    }
                    else
                    {
                        Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", inventory.items.Count, inventory.inventoryVolumeMax);
                        foreach (Item item in inventory.items)
                        {
                            if (item.itemData.id == 0)
                            {
                                countLongSword++;
                            }
                            else if (item.itemData.id == 1)
                            {
                                countShortSword++;
                            }
                            else if (item.itemData.id == 2)
                            {
                                countCloth++;
                            }
                            else if (item.itemData.id == 3)
                            {
                                countPlateArmor++;
                            }
                            else if (item.itemData.id == 4)
                            {
                                countRing++;
                            }
                            else if (item.itemData.id == 5)
                            {
                                countNecklace++;
                            }
                        }
                        if (countLongSword != 0)
                        {
                            Console.WriteLine("롱소드x{0}", countLongSword);
                        }
                        if (countShortSword != 0)
                        {
                            Console.WriteLine("숏소드x{0}", countShortSword);
                        }
                        if (countCloth != 0)
                        {
                            Console.WriteLine("천옷x{0}", countCloth);
                        }
                        if (countPlateArmor != 0)
                        {
                            Console.WriteLine("판금갑옷x{0}", countPlateArmor);
                        }
                        if (countRing != 0)
                        {
                            Console.WriteLine("반지x{0}", countRing);
                        }
                        if (countNecklace != 0)
                        {
                            Console.WriteLine("목걸이x{0}", countNecklace);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("잘못된 입력입니다.");
                }
            }
        }
    }
}
 
cs


Inventory.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class Inventory
    {
        public int inventoryVolumeMax;
        public List<Item> items;//아이템을 담을 List
 
        //생성자
        public Inventory(int inventoryVolumeMax)
        {
            this.items = new List<Item>();
            this.inventoryVolumeMax = inventoryVolumeMax;
 
            Console.WriteLine("인벤토리 생성(0/{0})", inventoryVolumeMax);
        }
 
        //추가
        public void AddItem(Item item)//아이템 추가
        {
            //아이템 추가
            this.items.Add(item);
        }
        //제거
        public void RemoveItem(Item founditem)
        {
            this.items.Remove(founditem);
        }
        //목록
    }
}
 
cs

Item.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class Item
    {
        //아이템 타입들
        public enum eItem
        {
            None = -1,
            Weapon = 0,
            Armor = 1,
            Accessory = 2
        }
 
        public ItemData itemData;
 
        //생성자
        public Item(ItemData itemData)
        {
            this.itemData = itemData;
            Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
        }
    }
}
 
cs

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
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    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# > 과제' 카테고리의 다른 글

<인벤토리> 저장, 불러오기 기능  (0) 2019.04.08
<IEnumerable과 IEnumerator> 내일 다시 확인하여 예제 준비할 것  (0) 2019.04.01
<인벤토리> 0.5차  (0) 2019.03.27
<객체와 개체의 차이>  (0) 2019.03.27
<코드읽기>  (0) 2019.03.26
:

이따가 분류

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
:

Boxing Unboxing

C#/아는것 2019. 3. 28. 18:19

구조체는 다른 구조체나 클래스에서 상속할 수 없음
구조체에 대한 상속을 흉내내기 위해서, 인터페이스를 사용한다.

박싱
값형식이나 구현된 객체를 Object형식이나 임의의 인터페이스 형식으로 변환하는 프로세스
값형식->참조형식으로 변환하여 힙에 저장, 박싱은 암시적이다(자동으로 해준당)

언박싱
인터페이스 형식을 객체나 값형식으로 변환하는 프로세스
참조형식->값형식, 언박싱은 명시적으로 변환해야만 한다.
단, 언박싱은 박싱을 한 참조형에게만 사용이 가능하다.

인터페이스(클래스의 기능의 정의가 포함되어있는 것)(상속 못하는 걸 상속하는 척하기위한 거)
인터페이스는 클래스 또는 구조체에서 구현할 수 있는 관련 기능에 그룹에 대한 정의가 포함되어 있다.
해당기능은 언어가 클래스의 여러 상속을 지원하지 않음 ->> 그래서 인터페스를 써야함

구조체는 다른 구조체나 클래스에서 상속할 수 없음
구조체에 대한 상속을 흉내내기 위해서, 인터페이스를 사용한다.

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Study_05

{

    class Program

    {

        static void Main(string[] args)

        {

            int i1 = 0;

            object o1 = i1;//boxing

            int j1 = (int)o1;//unboxing 

 

            Console.WriteLine("i1:{0} >>Boxing>> o1:{1} >>unboxsing>>j1:{2}", i1,o1,j1);

 

            short i2 = 0;

            object o2 = i2;

            //int j2 = (int)o2;//short형을 int 형으로 언박싱 불가!, 자료형을 맞춰줘야함

            //Console.WriteLine("i2:{0} >>Boxing>> o2:{1} >>unboxsing>>j2:{2}", i2, o2, j2);

 

            Console.ReadKey();

        }

    }

}

 

Colored by Color Scripter

cs

'C# > 아는것' 카테고리의 다른 글

IndexOf와 static 한정자  (0) 2019.03.27
<data type>var  (0) 2019.03.26
:

델리게이트

C#/모르는것 2019. 3. 28. 11:11

https://www.youtube.com/watch?v=B-yaWp900sQ

'C# > 모르는것' 카테고리의 다른 글

partial class  (0) 2019.07.12
<공부할 거 모아두기>하루에 하나라도 찾아보려고 노력하기  (0) 2019.03.26
:

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
:

<인벤토리> 0.5차

C#/과제 2019. 3. 27. 22:00


실행영상

App.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 inventory_00

{

    class App

    {

        Inventory inventory = new Inventory();

 

        public App()

        {

            while(true)

            {

                inventory.ConsoleMain();

                var input = Console.ReadLine();

                if (input == "1")

                {

                    //목록보기

                    Console.WriteLine("목록보기");

                    inventory.List();

                }

                else if (input == "2")

                {

                    //아이템 넣기

                    Console.WriteLine("아이템넣기");

                    inventory.AddItem();

                }

                else if (input == "3")

                {

                    //아이템 버리기

                    Console.WriteLine("아이템버리기");

                    inventory.RemoveItem();

                }

                else if (input == "0")

                {

                    Console.WriteLine("종료합니다.");

                    break;

                }

                else

                {

                    Console.WriteLine("잘못된 입력입니다.");

                }

            }

        }

    }

}

 

Colored by Color Scripter

cs


Inventory.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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace inventory_00

{

    class Inventory

    {

        List<string> inventory = new List<string>();

        Item redPotion = new Item("빨간물약""물약");

        Item bluePotion = new Item("파란포션""물약");

        Item longSword = new Item("롱소드""장비");

        Item shortSword = new Item("숏소드""장비");

 

        private int itemTotalStack = 0;

 

        public int ItemTotalStack

        {

            get

            {

                return itemTotalStack;

            }

            set

            {

                itemTotalStack = value;

            }

        }//itemTotalStack의 속성정의

 

        public void ConsoleMain()

        {

            Console.Write("==인벤토리==\n(1.목록/2.아이템 넣기/3.아이템 버리기/0.종료)(숫자입력):");

        }

 

        public void List()

        {

            if (itemTotalStack <= 0)

            {

                itemTotalStack = 0;

                Console.WriteLine("인벤토리에 아이템이 없습니다.");

            }

            else

            {

                redPotion.Stack = 0;

                bluePotion.Stack = 0;

                longSword.Stack = 0;

                shortSword.Stack = 0;

 

                foreach (var i in inventory)

                {

                    if (i == "빨간포션")

                    {

                        redPotion.Stack++;

                    }

                    else if (i == "파란포션")

                    {

                        bluePotion.Stack++;

                    }

                    else if (i == "롱소드")

                    {

                        longSword.Stack++;

                    }

                    else if (i == "숏소드")

                    {

                        shortSword.Stack++;

                    }

 

                }

                if (redPotion.Stack != 0)

                {

                    Console.WriteLine("빨간포션:{0}", redPotion.Stack);

                }

                if(bluePotion.Stack != 0)

                {

                    Console.WriteLine("파란포션:{0}", bluePotion.Stack);

                }

                if (longSword.Stack != 0)

                {

                    Console.WriteLine("롱소드:{0}", longSword.Stack);

                }

                if (shortSword.Stack != 0)

                {

                    Console.WriteLine("숏소드:{0}", shortSword.Stack);

                }

 

            }

        }

 

        public void AddItem()

        {

            Console.Write("(빨간포션, 파란포션, 롱소드, 숏소드)\n어떤 아이템을 넣으시겠습니까?:");

            var input = Console.ReadLine();

 

            if (input == "빨간포션")

            {

                Console.WriteLine("빨간포션을 넣었습니다.");

                inventory.Add("빨간포션");

                itemTotalStack++;

            }

            else if (input == "파란포션")

            {

                Console.WriteLine("파란포션을 넣었습니다.");

                inventory.Add("파란포션");

                itemTotalStack++;

            }

            else if (input == "롱소드")

            {

                Console.WriteLine("롱소드를 넣었습니다.");

                inventory.Add("롱소드");

                itemTotalStack++;

            }

            else if (input == "숏소드")

            {

                Console.WriteLine("숏소드를 넣었습니다.");

                inventory.Add("숏소드");

                itemTotalStack++;

            }

            else

            {

                Console.WriteLine("잘못된 입력입니다.");

            }

        }

 

        public void RemoveItem()

        {

            if (itemTotalStack == 0)

            {

                Console.WriteLine("버릴 아이템이 없습니다.");

            }

            else

            {

                Console.Write("(빨간포션, 파란포션, 롱소드, 숏소드)\n어떤 아이템을 버리시겠습니까?");

                var input = Console.ReadLine();

                if (input == "빨간포션")//빨간포션 버리기

                {

                    redPotion.Stack = 0;

                    foreach (var i in inventory)//빨간포션이 몇개인지 확인

                    {

                        if (i == "빨간포션")

                        {

                            redPotion.Stack++;

                        }

                    }

                    if (redPotion.Stack <= 0)

                    {

                        Console.WriteLine("빨간포션이 없습니다.");

                    }

                    else

                    {

                        Console.WriteLine("빨간포션를 버렸습니다.");

                        inventory.Remove("빨간포션");

                        itemTotalStack--;

                    }

                }

                else if (input == "파란포션")//파란포션 버리기

                {

                    bluePotion.Stack = 0;

                    foreach (var i in inventory)//파란포션 몇개인지 확인

                    {

                        if (i == "파란포션")

                        {

                            bluePotion.Stack++;

                        }

                    }

                    if (bluePotion.Stack <= 0)

                    {

                        Console.WriteLine("파란포션가 없습니다.");

                    }

                    else

                    {

                        Console.WriteLine("파란포션를 버렸습니다.");

                        inventory.Remove("파란포션");

                        itemTotalStack--;

                    }

                }

                else if (input == "롱소드")//롱소드 버리기

                {

                    longSword.Stack = 0;

                    foreach (var i in inventory)//롱소드가 몇개인지 확인

                    {

                        if (i == "롱소드")

                        {

                            longSword.Stack++;

                        }

                    }

                    if (longSword.Stack <= 0)

                    {

                        Console.WriteLine("롱소드가 없습니다.");

                    }

                    else

                    {

                        Console.WriteLine("롱소드를 버렸습니다.");

                        inventory.Remove("롱소드");

                        itemTotalStack--;

                    }

                }

                else if (input == "숏소드")//숏소드 버리기

                {

                    shortSword.Stack = 0;

                    foreach (var i in inventory)//숏소드 몇개인지 확인

                    {

                        if (i == "숏소드")

                        {

                            shortSword.Stack++;

                        }

                    }

                    if (shortSword.Stack <= 0)

                    {

                        Console.WriteLine("숏소드 없습니다.");

                    }

                    else

                    {

                        Console.WriteLine("숏소드 버렸습니다.");

                        inventory.Remove("숏소드");

                        itemTotalStack--;

                    }

                }

                else

                {

                    Console.WriteLine("잘못된 입력입니다.");

                }

            }

        }

    }

}

Colored by Color Scripter

cs

Item.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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace inventory_00

{

    class Item

    {

        private string name;

        private int stack;

        private string type;

 

        //속성정의 3개

        public string Name

        {

            get

            {

                return name;

            }

            set

            {

                name = value;

            }

        }

 

        public int Stack

        {

            get

            {

                return stack;

            }

            set

            {

                stack = value;

            }

        }

 

        public string Type

        {

            get

            {

                return type;

            }

            set

            {

                type = value;

            }

        }

 

        public Item(string name,string type)

        {

            this.name = name;

            this.type = type;

        }

    }

}

 

Colored by Color Scripter

cs

Program.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 inventory_00

{

    class Program

    {

        static void Main(string[] args)

        {

            new App();

 

            Console.ReadKey();

        }

    }

}

 

Colored by Color Scripter

cs
:

IndexOf와 static 한정자

C#/아는것 2019. 3. 27. 15:15

IndexOf는
static 한정자를 사용하므로
인스턴스.IndexOf가 아니라
원형
class명.IndexOf로 접근해야합니다.

ex)

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

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();

            Console.WriteLine(arrNames.GetValue(1));

            Console.WriteLine(Array.IndexOf(arrNames, "임꺽정"));

            //docs//array.GetValue()

 

        }

    }

}

 

Colored by Color Scripter

cs

'C# > 아는것' 카테고리의 다른 글

Boxing Unboxing  (0) 2019.03.28
<data type>var  (0) 2019.03.26
:

<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
: