'C#/실습'에 해당되는 글 45건

  1. 2019.09.20 09.20 줄넘기
  2. 2019.09.20 09.20 for문을 이용한 던전게임
  3. 2019.09.20 09.20 과일입력, 숫자입력, 문자열에 따옴표입력
  4. 2019.09.19 구구단 (convert를 이용한 string형식을 int 형식으로 변환, tryparse를 이용한 문자와 숫자 구분)
  5. 2019.09.19 2019/09/19 for 루프 1

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
 
:

구구단 (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
 
: