Array 인벤토리 잘모르겠는것
C#/Problems2019. 4. 3. 11:39
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_09 { class Inventory { public Item[] items = new Item[10]; public Inventory() { } //추가 public void AddItem(Item item) { for(int idx =0; idx <items.Length; idx++) { if(items[idx] == null) { items[idx] = item; Console.WriteLine("{0}이 생성되었습니다", items[idx].name); break; } } } //삭제 public void RemoveItem(string name) { Item removeitem = null; for (int i=0; i<items.Length; i++) { if(items[i].name == name) { removeitem = items[i]; items[i] = null; break; } } Console.WriteLine("{0}을 버렸습니다",removeitem.name); } /* //검색 public Item FindItem(string name) { } */ //추가 public void UpdateItem(ref Item item, string name) { } //출력 public void DisplayItems() { for (int i = 0; i < items.Length; i++) { if (items[i] != null) { Console.WriteLine("--> {0}",items[i].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 29 30 31 32 33 34 35 36 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_09 { class App { public App() { } public void Start() { Inventory inventory = new Inventory(); inventory.AddItem(new Item("두건")); //inventory.AddItem(null); inventory.AddItem(new Item("조종복")); inventory.AddItem(new Item("장갑")); inventory.AddItem(new Item("null")); inventory.RemoveItem("두건"); inventory.DisplayItems(); } } } | 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_09 { 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_09 { class Program { static void Main(string[] args) { App app = new App(); app.Start(); } } } | cs |
break;문의 습관화
ref
아이템 개수 추가 삭제
'C# > Problems' 카테고리의 다른 글
롤 애니비아 바론 싸움 (캐릭터생성오류) (0) | 2019.04.11 |
---|