ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2020-04-20 [리스트]
    C#/수업내용 2020. 4. 20. 18:56
    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace Study_012
    {
        class APP1
        {
            public APP1()
            {
                // //리스트 객체 생성 
                //List<string> list =  new List<string>();
                // //값을추가
                // list.Add("hong");
                // //리스트 길이
                // Console.WriteLine(list.Count);
                // //인덱스로 접근해서 가져오거나 설정할 수 있다.
                // //list[0] = "lim";
                // //index 요소는 카운트가 0 이랑 같거나 클경우 에러남 
                // //for문으로 요소 출력
     
                // list.Add("jang");
                // Console.WriteLine(list.Count);
     
                // for (int i = 0; i <list.Count; i++)
                // {
                //     Console.WriteLine(list[i]);
                // }
                // Console.WriteLine("-------------------------");
                // foreach(string name in list)
                // {
                //     Console.WriteLine(name);
                // }
     
                // //hong 문자열과 같은 요소를 찾는다
                // string foundName = "";
                // foreach(string name in list)
                // {
                //     if (name == "hong")
                //     {
                //         //찾은 경우 
                //         foundName = name;
                //     }
                //     break;
                // }
                // Console.WriteLine("----> {0}", foundName);
     
     
                // string foundname2 = list.Find(x => x == "hong"); // 사용한다 정도로만 사용하자 아직 이해할 내용 많음 
                // Console.WriteLine("fondName2 : {0}", foundname2); 
     
                // list.Remove("hong");
     
                // foreach (var name in list)
                // {
                //     Console.WriteLine(name);
                // }
                // Console.WriteLine("list.Count: {0}", list.Count);
     
     
                //리스트<Item> 객체 생성
                //Item
                // ㄴ 멤버 : name, amount
                //리스트에 Item 객체 추가
                //리스트에 있는 Item 객체의 name 과 amount를 출력 
                // 객체 3개정도
     
                // ex) list.Add(new Item("장검", 3));
     
                //List<Item> itemlist = new List<Item>();
                //itemlist.Add(new Item("롱소드", 4));
                //itemlist.Add(new Item("숏소드", 3));
                //itemlist.Add(new Item("롱보우", 2));
     
                //foreach(Item item in itemlist)
                //{
                //    Console.WriteLine("이름: {0} / 수량: {1}", item.name, item.amount);
                //}
     
                //// list 안에 item 찾기 (name이 "장검")
                //// 아이템을 찾았습니다: 장검 
     
                //foreach(Item item in itemlist)
                //{
                //    if(item.name == "롱소드")
                //    {
                //        Console.WriteLine("아이템을 찾았습니다 : {0}", item.name);
                //    }
                //}
                //Console.WriteLine("------------------------------------------");
                //for (int i = 0; i < itemlist.Count; i++)
                //{
                //    if(itemlist[i].name == "숏소드")
                //    {
                //        Console.WriteLine("아이템을 찾았습니다 : {0}", itemlist[i].name);
                //    }
                //}
                //Console.WriteLine("------------------------------------------");
                //// string foundname2 = list.Find(x => x == "hong");
                //Item foundItem = itemlist.Find(x => x.name == "롱소드");
                //Console.WriteLine("foundItem : {0} 찾음", foundItem.name);
                //Console.WriteLine("------------------------------------------");
     
     
                //장검 9개를 습득한걸로 
     
                //List<Item> itemlist = new List<Item>();
                //itemlist.Add(new Item("롱소드", 3));
                //itemlist.Add(new Item("롱소드", 3));
                //itemlist.Add(new Item("롱소드", 3));
     
                //인벤토리 안 리스트 안을 통해 관리 
                //Inventory inventory = new Inventory();
                //inventory.Add(new Item("롱소드", 3));
                //inventory.Add(new Item("롱소드", 2));
                //inventory.Add(new Item("롱소드", 3));
                //inventory.Add(new Item("숏소드", 1));
     
                //inventory.PrintAllItems();
     
                //inventory.GetItemByName("숏소드");
     
     
                List<Item> dropItemlist = new List<Item>();
                var Item = new Item("롱소드"10);
                var Item1 = new Item("숏소드"2);
                var Item2 = new Item("활"5);
     
                dropItemlist.Add(Item);
                dropItemlist.Add(Item1);
                dropItemlist.Add(Item2);
     
                Console.WriteLine("떨어져있는 아이템");
                Console.WriteLine("--------------------------");
                this.PrintDropItemList(dropItemlist);
             
                Console.WriteLine("소지중인 아이템");
                Console.WriteLine("--------------------------");
     
     
     
            }
     
            public void PrintDropItemList(List<Item> list)
            {
                int i = 0;
                foreach ( var item in list)
                {
                    if(i >= list.Count -1)
                    {
                        Console.WriteLine("{0}x{1}"item.name, item.amount);
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.Write("{0}x{1},"item.name, item.amount);
                    } 
                    i++;
                }
                Console.WriteLine("--------------------------");
     
            }
     
        }
    }
     
    ==========================================================================================
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace Study_012
    {
        class Item
        {
            public string name;
            public int amount;
            public Item(string name, int amount)
            {
                this.name = name;
                this.amount = amount;
               
            }
        }
    }
    =============================================================================================
     
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
     

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

    2020-04-28  (0) 2020.04.28
    2020-04-21 [json]  (0) 2020.04.21
    2020-04-20 [1차원 배열 활용]  (0) 2020.04.20
    2020-04-17 [1차원배열 ]  (0) 2020.04.17
    2020-04-16 [1차원배열 활용]  (0) 2020.04.16
Designed by Tistory.