'분류 전체보기'에 해당되는 글 139건

  1. 2019.09.19 문자열 표현방식
  2. 2019.09.19 구구단 (convert를 이용한 string형식을 int 형식으로 변환, tryparse를 이용한 문자와 숫자 구분)
  3. 2019.09.19 2019/09/19 for 루프 1

문자열 표현방식

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
 
: