10.10 class를 활용해 인벤토리와 기능 만들기
C#/실습 2019. 10. 10. 13:31예제:

코드:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class App
{
public App()
{
Item item1 = new Item(1, "장검");
Item item2 = new Item(2, "단검");
Item item3 = new Item(3, "활");
Inventory inventory = new Inventory();
inventory.AddItem(item1);
inventory.AddItem(item2);
inventory.AddItem(item3);
inventory.DisplayInventoryItemNames();
inventory.FindItemByName("장검");
inventory.RemoveItemByName("장검");
inventory.DisplayInventoryItemNames();
inventory.FindIndexOfItemByName("활");
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
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
|
class Inventory
{
public Item[] items = new Item[10];
int itemIndex = 0;
public Inventory()
{
}
public void AddItem(Item item)
{
items[itemIndex] = item;
itemIndex++;
}
public Item FindItemByName(string name)
{
foreach(Item item in items)
{
{
return item;
}
}
Console.WriteLine("아이템을 찾을 수 없습니다.");
return null;
}
public void RemoveItemByName(string name)
{
{
if (items[i] != null && items[i].name == name)
{
items[i] = null;
Console.WriteLine($"인벤토리에서 {name}을 삭제했습니다.");
return;
}
}
Console.WriteLine("아이템을 찾을 수 없습니다.");
}
public void DisplayInventoryItemNames()
{
string name=null;
Console.WriteLine("보유목록: ");
{
if (items[i] != null)
{
name = items[i].name;
Console.WriteLine(items[i].name);
}
}
if (name==null)
{
Console.WriteLine("아이템이 없습니다.");
}
}
public int FindIndexOfItemByName(string name)
{
foreach (Item item in items)
{
{
return item.id;
}
}
Console.WriteLine("해당 아이템을 찾을 수 없습니다.");
return 0;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
|
class Item
{
public int id;
public string name;
public Item(int id, string name)
{
this.id = id;
this.name = name;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'C# > 실습' 카테고리의 다른 글
10.15 Dictionary 활용해 업적 보상 UIPanel 만들기 (0) | 2019.10.15 |
---|---|
10.14 클오클 클랜원 UIPanel 만들기 (0) | 2019.10.14 |
10.07 class 활용해 1955버거 만들기 (0) | 2019.10.07 |
10.07 Class 활용해 BurgerSet 만들기 (0) | 2019.10.07 |
10.07 class 활용해 자동차만들기 (0) | 2019.10.07 |