심화 과제
- 중첩반복문을 활용하여 아래 그림처럼 출력하는 네가지 프로그램을 각각 작성하여 보자.
- Tip : Console.Write(" ");를 쓰면 빈 공백 하나를, Console.Write("*");을 쓰면 별 하나를 출력할 수 있다
1번 피라미드
for (int i = 1; i <= 5; i++)
{
for (int j = 0; j < i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
2번 피라미드
for (int i = 1; i <= 5; i++)
{
for (int j = 5; j > i; j--)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine("");
}
3번 피라미드
for (int i = 1; i <= 5; i++)
{
for (int j = 5; j >= i; j--)
{
Console.Write("*");
}
Console.WriteLine("");
}
4번 피라미드
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j < i; j++)
{
Console.Write(" ");
}
for (int k = 5; k >= i; k--)
{
Console.Write("*");
}
Console.WriteLine("");
}
다이아 몬드 출력
접근 방식
앞에 빈공간을 출력하고 그 뒤에 별을 출력하는 방식으로 접근했도 중간을 기점으로 위 아래를 나누어 따로 출력을 해주었습니다.
using System;
namespace Product
{
internal class Program
{
static void Main(string[] args)
{
int inputBoxInt = 0;
bool isCollect = false;
while (isCollect == false)
{
Console.Write("홀수와 1이 아닌 정수를 입력해 주세요 : ");
// 문자넣었는지 체크
isCollect = int.TryParse(Console.ReadLine() , out inputBoxInt);
// 문자를 넣지 않았다면 실행
if(isCollect == true)
{
// 홀수인지
if (inputBoxInt % 2 == 0)
{
Console.WriteLine("홀수를 입력해주세요");
isCollect = false;
}
else if (inputBoxInt == 1)
{
Console.WriteLine("1이 아닌 값을 입력해 주세요");
isCollect = false;
}
else
{
for (int i = 1; i <= inputBoxInt/2 + 1; i++)
{
for (int k = inputBoxInt / 2; k >= i ; k--)
{
Console.Write(" ");
}
for(int j = 1; j < i*2; j ++)
{
Console.Write("*");
}
Console.WriteLine(" ");
}
for (int a = 1; a <= inputBoxInt / 2; a++)
{
for (int b = 1; b <= a; b++)
{
Console.Write(" ");
}
for (int c = inputBoxInt; c > a*2 ; c --)
{
Console.Write("*");
}
Console.WriteLine("");
}
}
}
}
}
}
}
'경일게임아카데미 실습 > 11월' 카테고리의 다른 글
2024 - 11 - 29 (0) | 2024.11.29 |
---|---|
배열 과제 - 2차원 배열 활용 (2024 - 11- 28) (0) | 2024.11.28 |
과제 - 숫자 야구 게임 (2024 - 11 - 27) (0) | 2024.11.27 |