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 일때만 공백문자를 출력하도록 코드를 작성하였다.

 

: