'C#'에 해당되는 글 80건

  1. 2019.09.23 09.23 상점 구매하기 및 판매하기
  2. 2019.09.20 09.20 줄넘기
  3. 2019.09.20 09.20 for문을 이용한 던전게임
  4. 2019.09.20 09.20 과일입력, 숫자입력, 문자열에 따옴표입력
  5. 2019.09.20 WriteLine, ReadLine Method
  6. 2019.09.19 문자열 표현방식
  7. 2019.09.19 구구단 (convert를 이용한 string형식을 int 형식으로 변환, tryparse를 이용한 문자와 숫자 구분)
  8. 2019.09.19 2019/09/19 for 루프 1

09.23 상점 구매하기 및 판매하기

C#/실습 2019. 9. 23. 15:57

예제.

1.구매하기 기능과 판매하기 기능

구매및 판매

1.1 보유 금화 및 상점에 있는 아이템 종류, 가격및 수량 설정

상점페이지

1.2 구입하고자 아이템 종류와 수량 설정기능과 상점의 재고 상태 고려

1.3 보유 금화보다 많은 아이템 구매시:

1.4 상점의 재고량 보다 많은 아이템 구매시:

1.5 재고가 없는 아이템 구매시:

1.6 상점에 없는 아이템 선택시:

 

코드:

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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax05
{
    class Program
    {
        static void Main(string[] args)
        {
 
            string a = "장검";
            string b = "단검";
            string c = "활";
            string d = "도끼";
            int shopa = 5;//아이템 수량
            int shopb = 6;
            int shopc = 7;
            int shopd = 8;
            int a1 = 800;//아이템 가격
            int a2 = 550;
            int a3 = 760;
            int a4 = 810;
            int wallet = 5000;
            int ha1 = 0//보유 개수
            int ha2 = 0;
            int ha3 = 0;
            int ha4 = 0;
            while (true)
            {
            Restart:
                Console.WriteLine("1. 상품 구매하기");
                Console.WriteLine("2. 상품 판매하기");
                string choice = Console.ReadLine();
                if (choice == "1")//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@구매하기 시작
                {
                    Console.WriteLine("*** 상점 ***");
                    Console.WriteLine($"금화: {wallet}");
                    Console.WriteLine($"1. 장검: {a1}원, 보유 {shopa}개");
                    Console.WriteLine($"2. 단검: {a2}원, 보유 {shopb}개.");
                    Console.WriteLine($"3. 활: {a3}원, 보유{shopc}개.");
                    Console.WriteLine($"4. 도끼: {a4}원, 보유 {shopd}개.");
                    Console.Write("상점에서 구입 하고자 하는 아이템을 입력해주세요: ");
                    string input = Console.ReadLine();
                    if (input == "장검")
                    {
                        if (shopa >= 1)
                        {
                            if (wallet >= a1)
                            {
                                Console.WriteLine("------------------");
                                Console.Write("구입하고자 하는 갯수를 입력해주세요: ");
                                string ba1 = Console.ReadLine();
                                int ba1n = Convert.ToInt32(ba1);
                                if (shopa >= ba1n)
                                {
                                    if (wallet >= a1 * ba1n)
                                    {
                                        Console.WriteLine($"{input}을 {ba1n}개 구입했습니다. (-{a1 * ba1n})");
                                        wallet = wallet - (a1 * ba1n);
                                        ha1 = ha1 + ba1n;
                                        shopa = shopa - ba1n;
                                    }
                                    else
                                    {
                                        Console.WriteLine("금화가 부족합니다.");
                                    }
                                    Console.WriteLine("------------------");
                                }
                                else
                                {
                                    Console.WriteLine("재고가 부족합니다.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("금화가 부족합니다.");
                                Console.WriteLine("------------------");
                            }
                        }
                        else
                        {
                            Console.WriteLine("매진 되었습니다.");
                            Console.WriteLine("------------------");
                        }
                    }
                    else if (input == "단검")
                    {
                        if (shopb >= 1)
                        {
                            if (wallet >= a2)
                            {
                                Console.WriteLine("------------------");
                                Console.Write("구입하고자 하는 갯수를 입력해주세요: ");
                                string ba2 = Console.ReadLine();
                                int ba2n = Convert.ToInt32(ba2);
                                if (shopb >= ba2n)
                                {
                                    if (wallet >= a2 * ba2n)
                                    {
                                        Console.WriteLine($"{input}을 {ba2n}개 구입했습니다. (-{a2 * ba2n})");
                                        wallet = wallet - (a2 * ba2n);
                                        ha2 = ha2 + ba2n;
                                        shopb = shopb - ba2n;
                                    }
                                    else
                                    {
                                        Console.WriteLine("금화가 부족합니다.");
                                    }
                                    Console.WriteLine("------------------");
                                }
                                else
                                {
                                    Console.WriteLine("재고가 부족합니다.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("금화가 부족합니다.");
                                Console.WriteLine("------------------");
                            }
                        }
                        else
                        {
                            Console.WriteLine("매진 되었습니다.");
                            Console.WriteLine("------------------");
                        }
                    }
                    else if (input == "활")
                    {
                        if (shopc >= 1)
                        {
                            if (wallet >= a3)
                            {
                                Console.WriteLine("------------------");
                                Console.Write("구입하고자 하는 갯수를 입력해주세요: ");
                                string ba3 = Console.ReadLine();
                                int ba3n = Convert.ToInt32(ba3);
                                if (shopc >= ba3n)
                                {
                                    if (wallet >= a3 * ba3n)
                                    {
                                        Console.WriteLine($"{input}을 {ba3n}개 구입했습니다. (-{a3 * ba3n})");
                                        wallet = wallet - (a3 * ba3n);
                                        ha3 = ha3 + ba3n;
                                        shopc = shopc - ba3n;
                                    }
                                    else
                                    {
                                        Console.WriteLine("금화가 부족합니다.");
                                    }
                                    Console.WriteLine("------------------");
                                }
                                else
                                {
                                    Console.WriteLine("재고가 부족합니다.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("금화가 부족합니다.");
                                Console.WriteLine("------------------");
                            }
                        }
                        else
                        {
                            Console.WriteLine("매진 되었습니다.");
                            Console.WriteLine("------------------");
                        }
                    }
                    else if (input == "도끼")
                    {
                        if (shopd >= 1)
                        {
                            if (wallet >= a4)
                            {
                                Console.WriteLine("------------------");
                                Console.Write("구입하고자 하는 갯수를 입력해주세요: ");
                                string ba4 = Console.ReadLine();
                                int ba4n = Convert.ToInt32(ba4);
                                if (shopd >= ba4n)
                                {
                                    if (wallet >= a4 * ba4n)
                                    {
                                        Console.WriteLine($"{input}을 {ba4n}개 구입했습니다. (-{a4 * ba4n})");
                                        wallet = wallet - (a4 * ba4n);
                                        ha4 = ha4 + ba4n;
                                        shopd = shopd - ba4n;
                                    }
                                    else
                                    {
                                        Console.WriteLine("금화가 부족합니다.");
                                    }
                                    Console.WriteLine("------------------");
                                }
                                else
                                {
                                    Console.WriteLine("재고가 부족합니다.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("금화가 부족합니다.");
                                Console.WriteLine("------------------");
                            }
                        }
                        else
                        {
                            Console.WriteLine("매진 되었습니다.");
                            Console.WriteLine("------------------");
                        }
                    }
                    else
                    {
                        Console.WriteLine("------------------");
                        Console.WriteLine($"해당상품 (\"{input}\")(은)는 없습니다.");
                        Console.WriteLine("------------------");
                    }
                }// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 구매하기끝
                else if (choice == "2")
                {
                    Console.WriteLine($"금화: {wallet}");
                    Console.WriteLine("------------------");
                    Console.WriteLine("보유 목록:");
                    if (ha1 > 0)
                    {
                        Console.WriteLine($"{a} X {ha1}");
                    }
                    if (ha2 > 0)
                    {
                        Console.WriteLine($"{b} X {ha2}");
                    }
                    if (ha3 > 0)
                    {
                        Console.WriteLine($"{c} X {ha3}");
                    }
                    if (ha4 > 0)
                    {
                        Console.WriteLine($"{d} X {ha4}");
                    }
                    Console.Write("판매할 아이템을 입력해주세요: ");
                    string input = Console.ReadLine();
                    if (input == "장검")
                    {
                        if (ha1 >= 1)
                        {
                            Console.WriteLine("------------------");
                            Console.WriteLine("------------------");
                            Console.Write("판매할 수량을 입력해주세요: ");
                            string sa1 = Console.ReadLine();
                            int nsa1 = Convert.ToInt32(sa1);
                            if (ha1 >= nsa1)
                            {
                                ha1 = ha1 - nsa1;
                                Console.WriteLine($"{input}을 {nsa1}개 판매했습니다. (+{a1 * nsa1})");
                                wallet = wallet + (a1 * nsa1);
                                shopa = shopa + nsa1;
                            }
                            else
                            {
                                Console.WriteLine("더 이상 판매 할 수 없습니다.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("더 이상 판매 할 수 없습니다.");
                        }
 
                    }
                    else if (input == "단검")
                    {
                        if (ha2 >= 1)
                        {
                            Console.WriteLine("------------------");
                            Console.WriteLine("------------------");
                            Console.Write("판매할 수량을 입력해주세요: ");
                            string sa2 = Console.ReadLine();
                            int nsa2 = Convert.ToInt32(sa2);
                            if (ha2 >= nsa2)
                            {
                                ha2 = ha2 - nsa2;
                                Console.WriteLine($"{input}을 {nsa2}개 판매했습니다. (+{a2 * nsa2})");
                                wallet = wallet + (a2 * nsa2);
                                shopb = shopb + nsa2;
                            }
                            else
                            {
                                Console.WriteLine("더 이상 판매 할 수 없습니다.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("더 이상 판매 할 수 없습니다.");
                        }
                    }
                    else if (input == "활")
                    {
                        if (ha3 >= 1)
                        {
                            Console.WriteLine("------------------");
                            Console.WriteLine("------------------");
                            Console.Write("판매할 수량을 입력해주세요: ");
                            string sa3 = Console.ReadLine();
                            int nsa3 = Convert.ToInt32(sa3);
                            if (ha3 >= nsa3)
                            {
                                ha3 = ha3 - nsa3;
                                Console.WriteLine($"{input}을 {nsa3}개 판매했습니다. (+{a3 * nsa3})");
                                wallet = wallet + (a3 * nsa3);
                                shopc = shopc + nsa3;
                            }
                            else
                            {
                                Console.WriteLine("더 이상 판매 할 수 없습니다.");
                            }
 
                        }
                        else
                        {
                            Console.WriteLine("더 이상 판매 할 수 없습니다.");
                        }
                    }
                    else if (input == "도끼")
                    {
                        if (ha4 >= 1)
                        {
                            Console.WriteLine("------------------");
                            Console.WriteLine("------------------");
                            Console.Write("판매할 수량을 입력해주세요: ");
                            string sa4 = Console.ReadLine();
                            int nsa4 = Convert.ToInt32(sa4);
                            if (ha4 >= nsa4)
                            {
                                ha4 = ha4 - nsa4;
                                Console.WriteLine($"{input}을 {nsa4}개 판매했습니다. (+{a4 * nsa4})");
                                wallet = wallet + (a4 * nsa4);
                                shopd = shopd + nsa4;
                            }
                            else
                            {
                                Console.WriteLine("더 이상 판매 할 수 없습니다.");
                            }
 
                        }
                        else
                        {
                            Console.WriteLine("더 이상 판매 할 수 없습니다.");
                        }
 
                    }
                    else
                    {
                        Console.WriteLine("------------------");
                        Console.WriteLine($"해당상품 (\"{input}\")(은)는 없습니다.");
                        Console.WriteLine("------------------");
                    }
 
                }//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@판매 하기 끝
                else
                {
                    Console.WriteLine("잘못된 메뉴입니다.");
                    goto Restart;
 
                }
                Console.WriteLine($"금화: {wallet}");
                Console.WriteLine("------------------");
                Console.WriteLine("보유 목록:");
                if (ha1 > 0)
                {
                    Console.WriteLine($"{a} X {ha1}");
                }
                if (ha2 > 0)
                {
                    Console.WriteLine($"{b} X {ha2}");
                }
                if (ha3 > 0)
                {
                    Console.WriteLine($"{c} X {ha3}");
                }
                if (ha4 > 0)
                {
                    Console.WriteLine($"{d} X {ha4}");
                }
                Console.WriteLine("------------------");
                Console.WriteLine();
                Console.WriteLine();
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

'C# > 실습' 카테고리의 다른 글

switch 문  (0) 2019.09.26
09.24 최대공약수 구하기  (0) 2019.09.24
09.20 줄넘기  (0) 2019.09.20
09.20 for문을 이용한 던전게임  (0) 2019.09.20
09.20 과일입력, 숫자입력, 문자열에 따옴표입력  (0) 2019.09.20
:

09.20 줄넘기

C#/실습 2019. 9. 20. 17:08

예제:

d키 입력여부

D키 입력시:

다른키 입력시:

다른키 입력시

D키를 입력 할때까지 저 문구가 나온다.

줄넘기 횟수 입력시:

홀수 숫자는 초록색으로 나타냈고

줄넘기 횟수가 3의 배수인 경우에는 뒤에 ("야호!")를 붙였다.

코드:

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stntax03
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            { 
            Console.Write("줄넘기를 하려면 D 키를 입력 해주세요.");
            ConsoleKeyInfo keyinfo2 = Console.ReadKey();
            bool input = keyinfo2.KeyChar == 'd';
                //string a = keyinfo2.KeyChar.ToString(); //ConsoleKeyInfo 형태를 string 형태로 바꾸기
                Console.Clear();
                if (input)
                {
                    Console.Write("몇회 줄넘기를 할건가요?");
                    string strcount = Console.ReadLine();
                    int count = Convert.ToInt32(strcount);
                    for (int i = 1; i <= count; i++)
                    {
                        if (i % 2 == 0)
                        {
                            Console.Write($"줄넘기를 {i}회 했습니다.");
                        }
                        else
                        {
                            Console.Write("줄넘기를 ");
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write(i);
                            Console.ResetColor();
                            Console.Write("회 했습니다.");
                        }
                        if (i % 3 == 0)
                        {
                            Console.Write("(\"야호!\")");
                        }
                        Console.WriteLine();
                    }
                 break;
                }
                else
                {
                    Console.WriteLine("줄넘기를 하지 못했습니다");
                }
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

 

:

09.20 for문을 이용한 던전게임

C#/실습 2019. 9. 20. 11:27

예제:

코드:

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _09._20
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("케릭터의 이름을 입력해주세요: ");
            string characterName = Console.ReadLine();
            Console.Write("직업을 설정해주세요: ");
            string className = Console.ReadLine();
            Console.Write("체력을 설정해주세요: ");
            string strmaxHp = Console.ReadLine();
            int maxHp = Convert.ToInt32(strmaxHp);
            Console.Write("공격력을 설정해주세요: ");
            string strdmg = Console.ReadLine();
            int heroDamage = Convert.ToInt32(strdmg);
            float monsterDamage = 3.5f;
            float hp = maxHp;//현재 체력
            Console.WriteLine("----------------------");
            Console.WriteLine($"이름: {characterName}");
            Console.WriteLine($"직업: {className}");
            Console.WriteLine($"체력: {hp}/{maxHp}");
            Console.WriteLine($"공격력: {heroDamage}");
            Console.WriteLine("----------------------");
            Console.WriteLine("던전으로 들어갔습니다.");
            Console.WriteLine($"몬스터에게 공격(-{monsterDamage})을 받았습니다. ({hp = hp - monsterDamage}/{maxHp})");
            for (int i =1; i<4; i++)
            {
                Console.WriteLine($"몬스터에게 공격(-{monsterDamage})을 받았습니다. ({hp = hp - monsterDamage}/{maxHp})");
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
:

09.20 과일입력, 숫자입력, 문자열에 따옴표입력

C#/실습 2019. 9. 20. 10:21

예제:

0920예제

코드:

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _09._20
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("당신이 좋아하는 과일의 이름은 무엇입니까?  ");
            string fruit = Console.ReadLine();
            Console.WriteLine($"오! 당신은 {fruit}를 좋아하는군요!");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("*** 0보다 크고 100보다 작은 숫자 두개를 입력하고 출력 하기***");
            Console.Write("첫번째 숫자를 입력 해 주세요:  ");
            string strnum1 = Console.ReadLine();
            Console.Write("두번째 숫자를 입력 해 주세요:  ");
            string strnum2 = Console.ReadLine();
            Console.WriteLine($"당신이 입력한 숫자는 {strnum1}, {strnum2}입니다.");
            int num1 = Convert.ToInt32(strnum1);
            int num2 = Convert.ToInt32(strnum2);
            Console.WriteLine($"당신이 입력한 숫자의 합은 {num1 + num2}입니다.");
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
:

WriteLine, ReadLine Method

C#/API 2019. 9. 20. 09:45

코드:

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 _09._20
{
    class Program
    {
        static void Main(string[] args)
        {
            string Input = Console.ReadLine();
            Console.WriteLine(Input);
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

출력:

입력전
입력
출력

 

참조: https://docs.microsoft.com/ko-kr/dotnet/api/system.console.readline?view=netframework-4.8

:

문자열 표현방식

C#/Study 2019. 9. 19. 17:05

1. 연산자 +를 통한 단순 결합

예:

string a = 2;
            Console.WriteLine("5" + a = "7");

출력:

2+2=4

 

 

2. $- 문자열 보간 (직접 항목을 문자열 리터럴안에 주입하는것처럼 보인다)

예:

string a =2;

Console.WriteLine($"5 + {a} = 7")

출력:

5+2=7

 

 

3.합성 서식 문자열 (항목을 문자열 리터럴 밖에 내놓고 나열하는것처럼보인다. 중복되는 항목이 많을떄 쓰면 좋을것 같다.)

예:

string name1 = "Fred";

string name2 = "Tom";

Console.WriteLine("{0} and {1} are friends",name1,name2);

 

출력:

Fred and Tom are friends.

 

 

'C# > Study' 카테고리의 다른 글

박싱과 언박싱  (0) 2019.10.31
배열 내 요소중 최대값 구하기  (0) 2019.10.15
성적표만들기  (0) 2019.09.27
1차원 배열  (0) 2019.09.24
09.23 Row와 Column 설정하기  (0) 2019.09.23
:

구구단 (convert를 이용한 string형식을 int 형식으로 변환, tryparse를 이용한 문자와 숫자 구분)

C#/실습 2019. 9. 19. 16:08

구구단 예제:

콘솔창에 원하는 숫자를 입력하고 그 숫자에 따른 구구단을 9까지 나열하라

구구단1단
구구단123단

코드:

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax02
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.WriteLine("구구단중에 원하는 단을 입력해주세요. ");
            string name = Console.ReadLine();
            int number = 0;
            bool canConvert = int.TryParse(name, out number);
            if (canConvert == true)
            {
                for (int i = 1; i <= 9; i++)
                {
int a = Convert.ToInt32(name);
                    Console.WriteLine("{0} X {1} = {2}",name,i, a*i );
                }
            }
            else
            {
                Console.WriteLine($"{name}은 숫자가 아닙니다!");
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
 
 
 

string name = Console.ReadLine(); 을 이용해 콘솔창에 입력된 숫자를 변수에 대입

int a = Convert.ToInt32(name); 을 이용해 변수에 대입된 숫자(string형태)를 int32형태로 변환하고 그것을 변수a에 int형태로 대입

int number = 0; tryparse를 이용하기 위해 변수를 하나 더 만들어준다. (아직 배우지 못한 내용이라서 정확한 의미는 모르겠다. 참조: https://docs.microsoft.com/ko-kr/dotnet/api/system.int32.tryparse?view=netframework-4.8)

bool canConvert = int.TryParse(name, out number); bool형식의 변수 canConvert를 만들어 밑에 쓰여질 if문의 조건에 사용한다. 그리고 tryparse를 이용해 변수 name에 대입된 값이 int 형태로 변환가능하면 true, 불가능하면 false를 나타낸다

if (canConvert == true) --> 변수 canConvert가 true 이면(변수 name을 int형태로 변환가능) 본문을 실행

for (int i = 1; i <= 9; i++)

                {

                    Console.WriteLine("{0} X {1} = {2}",name,i, a*i );

                }

for 루트를 이용해 구구단의 반복되는 부분을 쉽게하고 합성 서식 문자열을 이용해 출력될 값을 정의해준다.

else

            {

                Console.WriteLine($"{name}은 숫자가 아닙니다!");

            }

else문을 이용해 변수 canConvert가 true가 아닐 경우(변수 name이 int형태로 변환 불가) 아래 본문을 실행한다. 이때 사용한 문자열 표현식은 특수문자 $를 이용한 문자열 이 보간된 문자열 리터럴이다. (참조:https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/tokens/interpolated)

'C# > 실습' 카테고리의 다른 글

09.23 상점 구매하기 및 판매하기  (0) 2019.09.23
09.20 줄넘기  (0) 2019.09.20
09.20 for문을 이용한 던전게임  (0) 2019.09.20
09.20 과일입력, 숫자입력, 문자열에 따옴표입력  (0) 2019.09.20
2019/09/19 for 루프  (1) 2019.09.19
:

2019/09/19 for 루프

C#/실습 2019. 9. 19. 10:58

문제 종합:

09/19 루프 예제 종합

코드:

           

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax01
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int counter=1; counter<=10; counter++)
            {
                Console.WriteLine("Hello World!");
            }
 
            Console.WriteLine("*** 날짜 ***");
            for (int counter=1; counter <=12; counter++)
            {
                Console.WriteLine(counter +"월");
            }
            Console.WriteLine("*** 구구단 2단 ***");
            for (int i = 1; i <= 9; i++)
            {
                Console.WriteLine($"2 X {i} = {i * 2}");
            }
            Console.WriteLine("*** 별찍기 ***");
            string a = "*";
            for (int i = 1; i<=5; i++)
            {
                Console.Write(a);
                Console.WriteLine();
                a = a + "*";
            }
            Console.WriteLine("*** 별찍기2 ***");
            string a2 = "*";
            for (int counter = 1; counter <= 4; counter++)
            {
                if (counter == 1)
                {
                    Console.Write("    ");
                }
                else if (counter == 2)
                {
                    Console.Write("   ");
                }
                else if (counter == 3)
                {
                    Console.Write("  ");
                }
                else if (counter == 4)
                {
                    Console.Write(" ");
                }
                Console.Write(a2);
                Console.WriteLine();
                a2 = a2 + "*"
            }
            Console.WriteLine("*** 별찍기3 ***");
            for (int counter = 1; counter <= 5; counter++)
            {
                Console.WriteLine("*****");
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
: