'전체 글'에 해당되는 글 276건

  1. 2019.09.19 2일차 구구단 중에 원하는 단을 입력해주세요. FormatException 에러 해결
  2. 2019.09.19 2일차 구구단 중에 원하는 단을 입력해주세요. FormatException 에러
  3. 2019.09.19 2일차 ***별찍기2***

2일차 구구단 중에 원하는 단을 입력해주세요. FormatException 에러 해결

Console Programming/C# Console 2019. 9. 19. 15:09

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

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)

        {

            int num = -1;

            bool repeat = true;

 

            

 

            while (repeat)

            {

                Console.Write("구구단중에 원하는 단을 입력해주세요. ");

                string input = Console.ReadLine();

                try

                {

                    num = Convert.ToInt32(input);

                    if (num < Int32.MaxValue)

                    {

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

                        {

                            Console.WriteLine($"{num} x {i} = {num * i}");

                        }

                    }

                    else

                    {

                        Console.WriteLine("num이 -2,147,483,648 and +2,147,483,647을 초과합니다.");

                    }

                }

                catch (FormatException)

                {

                    Console.WriteLine("입력된 문자열은 정수가 아닙니다.");

                }

                catch (OverflowException)

                {

                    Console.WriteLine("숫자가 Int32 규격에 맞지 않습니다.");

                }

                Console.Write("다시 하시겠습니까? (Y,N)");

 

                string go = Console.ReadLine();

                if (go.ToUpper() != "Y")

                {

                    repeat = false;

                }

 

            }

        }

    }

}

 

http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

:

2일차 구구단 중에 원하는 단을 입력해주세요. FormatException 에러

Console Programming/C# Console 2019. 9. 19. 14:39

구구단 중에 원하는 단을 입력받고, 그 단을 출력하는 프로그램이다.

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 Syntax02
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.Write("구구단중에 원하는 단을 입력해주세요. ");
            string input = Console.ReadLine();
               
            int num = Convert.ToInt32(input);
              
            for (int i = 1; i < 10; i++)
            {
                Console.WriteLine($"{num} x {i} = {num * i}");
               
            }
 
            
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 입력받은 문자열을 Convet.ToInt32를 이용하여 정수로 바꿨다.

 

실행결과

숫자를 입력하면 잘 돌아간다. 그러나.........
숫자가 아닌 값을 입력하면 이런 에러가 발생한다.

 

디버그를 눌러보면 다음과 같은 화면이 나타난다.

 

입력된 문자열의 형식이 잘못되었을 때의 예외를 처리하지 않았다.

:

2일차 ***별찍기2***

Console Programming/C# Console 2019. 9. 19. 11:33

원하는 출력결과는

 

*** 별찍기2 ***

    *

   **

  ***

 ****

 

 

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
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)
        {
           
            Console.WriteLine("*** 별찍기2 ***");
            for (int i=0; i< 4; i++)
            {
                for (int j = 0; j < i+1; j++)
                {
                    for (int k = 4; k > j; k--)
                    {
                        Console.Write(" ");
                    }
                    Console.Write("*");
                    
                }
                Console.WriteLine();
            }
            
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

19라인의 for문이 계속 돌아가기 때문에 매번 4번의 공백문자가 들어가게 되었다. 

 

수정된 코드

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
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)
        {
           
            Console.WriteLine("*** 별찍기2 ***");
            for (int i=0; i< 4; i++)
            {
                for (int j = 0; j < i+1; j++)
                {
                    if (j == 0)
                    {
                        for (int k = 4; k > i; k--)
                        {
                            Console.Write(" ");
                        }
                        Console.Write("*");
                    }
                    else
                        Console.Write("*");
                                      
                }
                Console.WriteLine();
            }
            
        }
    }
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

if문을 사용하여 j ==0 일때만 공백문자를 출력하도록 코드를 작성하였다.

 

: