정리 카테고리/실습_과제
실습_수량 제한 있는 가방에 아이템 줍기_배열사용
FinalNeo®
2018. 3. 12. 23:45
1.요구 사항
-배열 사용
-아이템 줍기
-아이템 버리기
-상태보기
-가방의 수량제한(다차면 더 못담는다.)
2.결과 화면 및 샘플
3.소스 코드
string[] backPack = new string[9]; string[] thingItem = new string[15]; thingItem[0] = "양날검"; thingItem[1] = "낡은검"; thingItem[2] = "녹슨검"; thingItem[3] = "도검"; thingItem[4] = "대검"; thingItem[5] = "단검"; thingItem[6] = "지팡이"; thingItem[7] = "방망이"; thingItem[8] = "몽둥이"; thingItem[9] = "사탕"; thingItem[10] = "막대"; thingItem[11] = "판자"; thingItem[12] = "못"; thingItem[13] = "망치"; thingItem[14] = "연필"; inpuutCommand: Console.WriteLine(); Console.WriteLine("원하는 명령어를 입력하세요."); Console.WriteLine("보기 \n?? 줍기 \n?? 버리기 \n소지품 \n끝"); Console.Write(">"); string check; check = Console.ReadLine(); Console.Clear(); if(check.Contains(" 줍기")) { check = check.Replace(" 줍기", ""); if (Array.IndexOf(thingItem, check) > -1 && Array.IndexOf(thingItem, check) < thingItem.Length) { for (int j = 0; j < backPack.Length; j++) { if(backPack[j] == ""|| backPack[j] == null) { backPack[j] = check; thingItem[Array.IndexOf(thingItem, check)] = ""; Console.WriteLine(check+"를 주었습니다."); goto inpuutCommand; } } Console.WriteLine("가방이 꽉차서 더이상 넣을수가 없네요."); } } if (check.Contains(" 버리기")) { check = check.Replace(" 버리기", ""); if (Array.IndexOf(backPack, check) > -1 && Array.IndexOf(backPack, check) < backPack.Length) { for (int j = 0; j < thingItem.Length; j++) { if (thingItem[j] == "") { thingItem[j] = check; backPack[Array.IndexOf(backPack, check)] = ""; Console.WriteLine(check + "를 버렸습니다."); goto inpuutCommand; } } } } if (check.Equals("소지품")) { for (int j = 0; j < backPack.Length; j++) { if (backPack[j] == "" || backPack[j] ==null) ; else Console.WriteLine(backPack[j]); } } if (check.Equals("보기")) { for (int j = 0; j < thingItem.Length; j++) { if (thingItem[j] == "") ; else Console.WriteLine(thingItem[j]); } } if (check == "끝") goto endBye; goto inpuutCommand; endBye: Console.Write("아무키나 입력하세요"); Console.ReadKey();