1. 장바구니 프로그램.
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
|
//장바구니 프로그램.
string fruitList = "";
int max = 5, now = 0;
for(; ; )
{
Console.Write("메뉴 (1 : 입력, 2 : 삭제, 3 : 목록, 4 : 종료). ");
string input = Console.ReadLine();
if (input == "1")
{
//입력 하는 부분.
if (max <= now)
{
Console.WriteLine("물건을 더이상 담을 수 없습니다. ({0} / {1}). \n", now, max);
}
else
{
Console.Write("장바구니에 담을 물품을 입력 하세요. ");
string buff = Console.ReadLine().Trim();
fruitList += ", " + buff;
Console.WriteLine("{0}을 장바구니에 담았습니다. ({1} / {2})\n", buff, ++now, max);
}
}
else if(input == "2")
{
//제거하는 부분.
//얼기설기 구현.
Console.WriteLine("{0}", fruitList.Substring(2).Trim());
Console.Write("제거할 물건을 입력하세요. ");
string buff = Console.ReadLine().Trim();
if (fruitList.Contains(buff))
{
int index = fruitList.IndexOf(buff[0]);
{
}
Console.WriteLine("{0} 제거가 완료 되었습니다. ({1} / {2})\n", buff, --now, max);
}
else
{
Console.WriteLine("제거할 수 없습니다.\n");
}
}
else
{
Console.WriteLine("제거할 물건이 없습니다.\n");
}
}
else if(input == "3")
{
//목록 보여주는 부분.
Console.WriteLine("--------------------------------------\n장바구니\n--------------------------------------\n{0}\n", fruitList.Substring(2).Trim());
else
{
Console.WriteLine("장바구니에 물건이 없습니다.\n");
}
}
else if(input == "4")
{
//종료하는 부분.
Console.WriteLine("프로그램을 종료합니다.");
break;
}
else
{
Console.WriteLine("실행할 수 없는 명령입니다. \n");
}
}
|
1번 메뉴 : 장바구니에 물건 집어넣기.
2번 메뉴 : 장바구니에 있는 물건 삭제.
3번 메뉴 : 목록 전부 보여주기.
4번 메뉴 : 프로그램 종료.
물건 집어넣기
', '로 문자열을 구분하는데에 ", " concat을 문자의 앞으로 했습니다. 목록 출력 시 Substring(int startIndex) 함수를 사용
하는데 시작 인덱스부터 끝까지 전부 가져오고 "apple, banana" => ", apple, banana" 이런 식으로 바꿔 위 함수를 편하게 사용하기 위함입니다.
물건 삭제 기능
Trim() 함수를 자주 사용한 것을 볼 수 있는데 이는 앞뒤의 공백제거로 원하는 단어만 가져오게 하기 위함이고
string buff에 삭제할 물건을 입력 받고 fruitList.Contains(buff) 사용, 이는 쉼표로 구분된 fruitList 목록에서
buff 내용의 문자열이 있는지 검사해줍니다. 있다면 true 없으면 false 이 결과로 해당 문자열이 있는지 확인하며 remove함수로 해당 문자열을 제거했습니다. 아직 제거 기능이 완벽하지 않아 배열을 배운 후에 다시 올리겠습니다.
오늘 했던 전체 코드 입니다.
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_003
{
class App
{
public App()
{
//2020 - 04 - 06
Console.WriteLine("2020 - 04 - 06\n-----------------------------------------\n");
// Tutiroal
string name = "mark";
var date = DateTime.Now;
Console.WriteLine("hello, {0}! today is {1}, it's {2:hh:mm} now.", name, date.DayOfWeek, date);
Console.WriteLine($"hello, {name}! today is {date.DayOfWeek}, it's {date:hh:mm} now.");
Console.WriteLine("{0}", filename1);
Console.WriteLine($"{filename2} \n\n");
var a = 10;
int b = 10;
for (string fruit = ""; fruit != "종료"; ){
Console.Write("좋아하는 과일을 입력 하세요. ");
fruit = Console.ReadLine();
Console.ReadLine();
Console.Clear();
if (fruit != "종료")
Console.WriteLine("{0}을 좋아하시는군요. ", fruit);
}
Console.WriteLine("종료 했습니다.");
//do while문 사용 과일.
string fruit2 = "";
do
{
Console.Write("좋아하는 과일을 입력 하세요. ");
fruit2 = Console.ReadLine();
if (fruit2 != "종료")
Console.WriteLine("{0}을 좋아하시는군요. ", fruit2);
} while (fruit2 != "종료");
Console.WriteLine("종료 했습니다.");
//좋아하는 과일 목록 뽑기.
string fruitList = "";
for (; ; )
{
Console.Write("좋아하는 과일을 입력 하세요. ");
string buff = Console.ReadLine();
if(buff == "목록")
{
Console.WriteLine(fruitList.Substring(2));
else
Console.WriteLine("목록이 없습니다.");
}
else if(buff == "종료")
{
Console.WriteLine("종료 했습니다.");
break;
}
else
{
fruitList += ", " + buff;
Console.WriteLine("{0}을 좋아하시는군요", buff);
}
Console.WriteLine();
}
//장바구니 프로그램.
fruitList = "";
int max = 5, now = 0;
for(; ; )
{
Console.Write("메뉴 (1 : 입력, 2 : 삭제, 3 : 목록, 4 : 종료). ");
string input = Console.ReadLine();
if (input == "1")
{
//입력 하는 부분.
if (max <= now)
{
Console.WriteLine("물건을 더이상 담을 수 없습니다. ({0} / {1}). \n", now, max);
}
else
{
Console.Write("장바구니에 담을 물품을 입력 하세요. ");
string buff = Console.ReadLine().Trim();
fruitList += ", " + buff;
Console.WriteLine("{0}을 장바구니에 담았습니다. ({1} / {2})\n", buff, ++now, max);
}
}
else if(input == "2")
{
//제거하는 부분.
//얼기설기 구현.
Console.WriteLine("{0}", fruitList.Substring(2).Trim());
Console.Write("제거할 물건을 입력하세요. ");
string buff = Console.ReadLine().Trim();
if (fruitList.Contains(buff))
{
int index = fruitList.IndexOf(buff[0]);
{
}
Console.WriteLine("{0} 제거가 완료 되었습니다. ({1} / {2})\n", buff, --now, max);
}
else
{
Console.WriteLine("제거할 수 없습니다.\n");
}
}
else
{
Console.WriteLine("제거할 물건이 없습니다.\n");
}
}
else if(input == "3")
{
//목록 보여주는 부분.
Console.WriteLine("--------------------------------------\n장바구니\n--------------------------------------\n{0}\n", fruitList.Substring(2).Trim());
else
{
Console.WriteLine("장바구니에 물건이 없습니다.\n");
}
}
else if(input == "4")
{
//종료하는 부분.
Console.WriteLine("프로그램을 종료합니다.");
break;
}
else
{
Console.WriteLine("실행할 수 없는 명령입니다. \n");
}
}
//빵먹는 프로그램.
int count = 10;
Console.WriteLine("방의 갯수 : {0}\n", count);
for (; ; )
{
Console.Write("'먹어'를 입력하세요. ");
string input = Console.ReadLine();
if(input == "먹어")
{
if (count < 1)
{
Console.WriteLine("더이상 빵이 없네요..");
break;
}
else
{
Console.WriteLine("빵을 먹었습니다. {0}개 남았습니다.", --count);
}
}
else
Console.WriteLine("ㄴㄴ");
}
//아이스크림 먹기.
int bar1 = 0, bar2 = 0;
for (; ; )
{
Console.Write("어떤 아이스크림(1 : 죠스바, 2 : 누가바)을 먹을겁니까. ");
int type = 0;
bool parseable = int.TryParse(Console.ReadLine(), out type);
if (type == 1 || type == 2)
{
if(type == 1)
{
bar1++;
Console.WriteLine("그동안 죠스바를 {0}개 먹었습니다.", bar1);
}
else
{
bar2++;
Console.WriteLine("그동안 누가바를 {0}개 먹었습니다.", bar2);
}
}
else
{
Console.WriteLine("먹을 수 없습니다.");
}
}
//가위바위보
string[] game = { "가위", "바위", "보" };
int win = 0, lose = 0, draw = 0;
for (; ; )
{
Console.Write("1. 가위, 2. 바위, 3. 보, 4. 종료 : ");
string user = Console.ReadLine();
int userNum;
if (user == "1" || user == "2" || user == "3" || user == "4")
{
//입력이 정상적일 때.
if (user == "4") {
Console.WriteLine("게임을 종료합니다.");
break;
}
userNum = int.Parse(user);
Console.WriteLine("당신은 {0}을 냈습니다.", game[userNum - 1]);
Console.WriteLine("컴은 {0}을 냈습니다.", game[comp - 1]);
Console.WriteLine("==========================");
if (userNum == comp)
{
draw++;
Console.WriteLine("비겼습니다.");
}
else if (userNum - comp == 1 || userNum - comp == -2)
{
win++;
Console.WriteLine("이겼습니다.");
}
else
{
lose++;
Console.WriteLine("졌습니다.");
}
Console.WriteLine("승리 : {0}, 패배 : {1}, 무승부 : {2}, 총게임 : {3}\n", win, lose, draw, (win + lose + draw));
}
else
{
//입력이 비정상적일 때.
Console.WriteLine("입력할 수 없습니다. \n");
continue;
}
}
//스트링 게임, 홀길동의 모험.
int minAttack = 1;
int maxAttack = 5;
int heroAttack = 0;
int monsterMaxHp = 10;
int monsterCurrentHp = monsterMaxHp;
Console.Write("캐릭터 이름은 무엇입니까? ");
string heroName = Console.ReadLine();
for(; ; )
{
Console.Write("\n캐릭터 공격력을 설정해 주세요. ({0} ~ {1})", minAttack, maxAttack);
bool parseable = int.TryParse(Console.ReadLine(), out heroAttack);
if(parseable && heroAttack < 6 && heroAttack > 0)
{
Console.WriteLine("\n{0}님이 입장 하셨습니다. (공격력 : {1})", heroName, heroAttack);
Console.WriteLine("몬스터의 체력은 {0}입니다.\n", monsterMaxHp);
break;
}
else
{
Console.WriteLine("범위를 벗어났습니다. ");
}
}
for( ; ; )
{
Console.Write("몬스터를 공격 하시려면 '공격' 이라고 입력하세요. ");
string input = Console.ReadLine();
if(input == "공격")
{
Console.WriteLine("당신은 몬스터를 쳤습니다.");
monsterCurrentHp -= heroAttack;
Console.WriteLine("몬스터의 체력 : {0} / {1}\n", monsterCurrentHp, monsterMaxHp);
if(monsterCurrentHp <= 0)
{
Console.WriteLine("몬스터가 쓰러졌습니다.");
break;
}
}
else
{
Console.WriteLine("잘못된 명령어 입니다.\n");
}
}
}
}
}
|
Study_006-1 (0) | 2020.04.09 |
---|---|
Study_005 (0) | 2020.04.08 |
Study004 (0) | 2020.04.08 |
Study_002-2 (0) | 2020.04.03 |
Study_002-1 (0) | 2020.04.03 |