728x90
1. ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ for๋ฌธ์ ์ด์ฉํ์ฌ ์์ฑํ์ธ์.
*
**
***
****
*****
for (int i=1; i<=5; i++)
{
for(int j=0; j<i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
2. ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ for๋ฌธ์ ์ด์ฉํ์ฌ ์์ฑํ์ธ์.
*****
****
***
**
*
for (int i=5; i>=1; i--)
{
for(int j=0; j<i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
3. 1๋ฒ๊ณผ 2๋ฒ์ for๋ฌธ ๋์ while๋ฌธ๊ณผ do๋ฌธ์ผ๋ก ๋ฐ๊ฟ์ ๊ฐ๊ฐ ์์ฑํ์ธ์.
while๋ฌธ ์ฌ์ฉ
int i = 1;
while (i <= 5){
int j = 0;
while (j < i)
{
Console.Write("*");
j++;
}
Console.WriteLine();
i++;
}
int n = 5;
while (n >= 1) {
int m = 0;
while (m < n)
{
Console.Write("*");
m++;
}
Console.WriteLine();
n--;
}
do๋ฌธ ์ฌ์ฉ
int i = 1;
do {
int j = 0;
while (j < i)
{
Console.Write("*");
j++;
}
Console.WriteLine();
i++;
} while (i <= 5);
int n = 5;
do
{
int m = 0;
while (m < n)
{
Console.Write("*");
m++;
}
Console.WriteLine();
n--;
} while (n >= 1);
4. ์ฌ์ฉ์๋ก๋ถํฐ ์ ๋ ฅ๋ฐ์ ํ์๋งํผ ๋ณ์ ๋ฐ๋ณต ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์ธ์. ๋จ, ์ ๋ ฅ๋ฐ์ ์๊ฐ 0๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ๊ฒฝ์ฐ ์๋ฌ๋ฉ์ธ์ง๋ฅผ ๋์ฐ๊ณ ์ข ๋ฃํฉ๋๋ค.
Console.Write("๋ฐ๋ณต ํ์๋ฅผ ์
๋ ฅํ์ธ์: ");
string input = Console.ReadLine();
if (!int.TryParse(input, out int n))
{
Console.WriteLine("์ซ์๋ง ์
๋ ฅ ๊ฐ๋ฅํฉ๋๋ค.");
}
else
{
if (n < 0)
{
Console.WriteLine("์์๋ง ์
๋ ฅ ๊ฐ๋ฅํฉ๋๋ค.");
}
else
{
for (int i = 0; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}728x90
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [์ด๊ฒ์ด C#์ด๋ค] Chapter06: ์ฐ์ต๋ฌธ์ (0) | 2023.08.16 |
|---|---|
| [C#] ์ฐธ์กฐ ๋งค๊ฐ๋ณ์ ์ ๋ฌ, ์ฌ๋ฌ ๊ฐ์ ๊ฐreturn, ๊ฐ๋ณ ๊ฐ์์ ์ธ์, ์ ํ์ ์ธ์ (0) | 2023.08.16 |
| [C#] switch๋ฌธ๊ณผ switch์ (0) | 2023.08.16 |
| [์ด๊ฒ์ด C#์ด๋ค] Chapter4 ์ฐ์ต๋ฌธ์ (0) | 2023.08.16 |
| [C#] null ์กฐ๊ฑด๋ถ ์ฐ์ฐ์, ๋ณํฉ ์ฐ์ฐ์ (0) | 2023.08.16 |