C# - ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ - ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ ์์ ์ ๋ณต - ๊ธฐ์ด ๊ฐ๋ ๋ถํฐ ์ค์ต๊น์ง + ์ผํญ ์ฐ์ฐ์
๐ ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ 1 - if, else if, ์ค์ฒฉ ์กฐ๊ฑด๋ฌธ, switch, ์ผํญ ์ฐ์ฐ์
๐ ์กฐ๊ฑด๋ฌธ ๊ฐ์
์กฐ๊ฑด๋ฌธ์ ์ฃผ์ด์ง ์กฐ๊ฑด์์ ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ํ๋ก๊ทธ๋จ์ ์ ์ด ํ๋ฆ์ ๋ณ๊ฒฝํ๋ ์ ์ด๋ฌธ์ด๋ค.
if ๋ฌธ
if ๋ฌธ์ ์กฐ๊ฑด์์ด ์ฐธ์ผ ๊ฒฝ์ฐ์๋ง ํด๋น ๋ธ๋ก์ ์คํํ๋ค.
if (์กฐ๊ฑด์)
{
// ์กฐ๊ฑด์์ด ์ฐธ์ผ ๊ฒฝ์ฐ ์คํํ ์ฝ๋
}
ํ ์ค์ผ ๊ฒฝ์ฐ ์ค๊ดํธ ์๋ต ๊ฐ๋ฅ:
if (์กฐ๊ฑด์)
Console.WriteLine("์คํ");
์์:
int playerScore = 80;
if (playerScore >= 70)
{
Console.WriteLine("ํ๋ ์ด์ด์ ์ ์๋ 70์ ์ด์์
๋๋ค. ํฉ๊ฒฉ์
๋๋ค!");
}
Console.WriteLine("ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ๋ฉ๋๋ค.");
else ๋ฌธ
if ๋ฌธ์ ์กฐ๊ฑด์ด ๊ฑฐ์ง์ผ ๊ฒฝ์ฐ ์คํํ ์ฝ๋๋ฅผ ์ง์ ํ ๋ ์ฌ์ฉํ๋ค.
if (์กฐ๊ฑด์)
{
// ์กฐ๊ฑด์์ด ์ฐธ์ผ ๊ฒฝ์ฐ
}
else
{
// ์กฐ๊ฑด์์ด ๊ฑฐ์ง์ผ ๊ฒฝ์ฐ
}
์์:
int itemCount = 5;
string itemName = "HP ํฌ์
";
if (itemCount > 0)
{
Console.WriteLine($"๋ณด์ ํ {itemName}์ ์๋: {itemCount}");
}
else
{
Console.WriteLine($"๋ณด์ ํ {itemName}์ด ์์ต๋๋ค.");
}
else if ๋ฌธ
์กฐ๊ฑด์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ ์์ฐจ์ ์ผ๋ก ์กฐ๊ฑด์์ ๊ฒ์ฌํ ์ ์๋๋ก ํ๋ค.
if (์กฐ๊ฑด์1)
{
// ์กฐ๊ฑด์1์ด ์ฐธ
}
else if (์กฐ๊ฑด์2)
{
// ์กฐ๊ฑด์2๊ฐ ์ฐธ
}
else
{
// ๋ชจ๋ ์กฐ๊ฑด์ด ๊ฑฐ์ง
}
์์:
int playerScore = 100;
string playerRank = "";
if (playerScore >= 90)
{
playerRank = "Diamond";
}
else if (playerScore >= 80)
{
playerRank = "Platinum";
}
else if (playerScore >= 70)
{
playerRank = "Gold";
}
else if (playerScore >= 60)
{
playerRank = "Silver";
}
else
{
playerRank = "Bronze";
}
Console.WriteLine("ํ๋ ์ด์ด์ ๋ฑ๊ธ์ " + playerRank + "์
๋๋ค.");
์ค์ฒฉ ์กฐ๊ฑด๋ฌธ
์กฐ๊ฑด๋ฌธ ๋ด๋ถ์ ๋ ๋ค๋ฅธ ์กฐ๊ฑด๋ฌธ์ ํฌํจํ ๊ตฌ์กฐ๋ก, ๋ณต์กํ ๋ถ๊ธฐ ์ฒ๋ฆฌ์ ํ์ฉ๋๋ค.
์์:
int itemLevel = 3;
string itemType = "Weapon";
if (itemType == "Weapon")
{
if (itemLevel == 1)
{
Console.WriteLine("๊ณต๊ฒฉ๋ ฅ์ด 10 ์ฆ๊ฐํ์ต๋๋ค.");
}
else if (itemLevel == 2)
{
Console.WriteLine("๊ณต๊ฒฉ๋ ฅ์ด 20 ์ฆ๊ฐํ์ต๋๋ค.");
}
else
{
Console.WriteLine("์๋ชป๋ ์์ดํ
๋ ๋ฒจ์
๋๋ค.");
}
}
else if (itemType == "Armor")
{
if (itemLevel == 1)
{
Console.WriteLine("๋ฐฉ์ด๋ ฅ์ด 10 ์ฆ๊ฐํ์ต๋๋ค.");
}
else if (itemLevel == 2)
{
Console.WriteLine("๋ฐฉ์ด๋ ฅ์ด 20 ์ฆ๊ฐํ์ต๋๋ค.");
}
else
{
Console.WriteLine("์๋ชป๋ ์์ดํ
๋ ๋ฒจ์
๋๋ค.");
}
}
else
{
Console.WriteLine("์๋ชป๋ ์์ดํ
์ข
๋ฅ์
๋๋ค.");
}
switch ๋ฌธ
๋ณ์๋ ์์ ๊ฒฐ๊ณผ ๊ฐ์ ๋ฐ๋ผ ์คํํ ์ฝ๋๋ฅผ ๊ตฌ๋ถํ๋ ์กฐ๊ฑด๋ฌธ์ด๋ค.
switch (๋ณ์)
{
case ๊ฐ1:
// ์คํ ์ฝ๋
break;
case ๊ฐ2:
// ์คํ ์ฝ๋
break;
default:
// ์ผ์นํ๋ case๊ฐ ์์ ๋
break;
}
์์:
Console.WriteLine("1: ์ ์ฌ / 2: ๋ง๋ฒ์ฌ / 3: ๊ถ์");
Console.Write("์ง์
์ ์ ํํ์ธ์: ");
string job = Console.ReadLine();
switch (job)
{
case "1":
Console.WriteLine("์ ์ฌ๋ฅผ ์ ํํ์
จ์ต๋๋ค.");
break;
case "2":
Console.WriteLine("๋ง๋ฒ์ฌ๋ฅผ ์ ํํ์
จ์ต๋๋ค.");
break;
case "3":
Console.WriteLine("๊ถ์๋ฅผ ์ ํํ์
จ์ต๋๋ค.");
break;
default:
Console.WriteLine("์ฌ๋ฐ๋ฅธ ๊ฐ์ ์
๋ ฅํด์ฃผ์ธ์.");
break;
}
์ผํญ ์ฐ์ฐ์
์กฐ๊ฑด์์ ๋ฐ๋ผ ๋ ๊ฐ ์ค ํ๋๋ฅผ ์ ํํ๋ ์ฐ์ฐ์์ด๋ค.
(์กฐ๊ฑด์) ? ์ฐธ์ผ ๋ ๊ฐ : ๊ฑฐ์ง์ผ ๋ ๊ฐ;
์์:
int currentExp = 1200;
int requiredExp = 2000;
string result = (currentExp >= requiredExp) ? "๋ ๋ฒจ์
๊ฐ๋ฅ" : "๋ ๋ฒจ์
๋ถ๊ฐ๋ฅ";
Console.WriteLine(result);
๋์ผ ๋ก์ง์ if-else ๋ฌธ์ผ๋ก ํํํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค:
if (currentExp >= requiredExp)
{
Console.WriteLine("๋ ๋ฒจ์
๊ฐ๋ฅ");
}
else
{
Console.WriteLine("๋ ๋ฒจ์
๋ถ๊ฐ๋ฅ");
}
๐ ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ 2 — ์กฐ๊ฑด๋ฌธ ์ฌํ ์ค์ต
๐ ์ง์ / ํ์ ํ๋ณ
% ๋๋จธ์ง ์ฐ์ฐ์๋ฅผ ์ด์ฉํ์ฌ ์
๋ ฅ๋ฐ์ ์ ์๊ฐ ์ง์์ธ์ง ํ์์ธ์ง ํ๋ณํ ์ ์๋ค.
2๋ก ๋๋์์ ๋ ๋๋จธ์ง๊ฐ 0์ด๋ฉด ์ง์, ์๋๋ฉด ํ์์ด๋ค.
Console.Write("๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์: ");
int number = int.Parse(Console.ReadLine());
if (number % 2 == 0)
{
Console.WriteLine("์ง์์
๋๋ค.");
}
else
{
Console.WriteLine("ํ์์
๋๋ค.");
}
๐ ๋ฑ๊ธ ์ถ๋ ฅ (switch ํ์ฉ)
๊ธฐ์กด else if ๋ฐฉ์ ๋์ switch ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ ์๋ฅผ ๋ฑ๊ธ์ผ๋ก ๋ณํํ๋ค.
์ ์ ์ ์๋ฅผ 10์ผ๋ก ๋๋ ๋ชซ์ ๊ธฐ์ค์ผ๋ก ๋ถ๊ธฐ๋ฅผ ๋๋๋ค.
int playerScore = 100;
string playerRank = "";
switch (playerScore / 10)
{
case 10:
case 9:
playerRank = "Diamond";
break;
case 8:
playerRank = "Platinum";
break;
case 7:
playerRank = "Gold";
break;
case 6:
playerRank = "Silver";
break;
default:
playerRank = "Bronze";
break;
}
Console.WriteLine("ํ๋ ์ด์ด์ ๋ฑ๊ธ์ " + playerRank + "์
๋๋ค.");
๐ ๋ก๊ทธ์ธ ํ๋ก๊ทธ๋จ
๋
ผ๋ฆฌ ์ฐ์ฐ์ &&, ||๋ฅผ ํ์ฉํด ์์ด๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํ๋ค.
๋ชจ๋ ์ผ์นํด์ผ ๋ก๊ทธ์ธ ์ฑ๊ณต์ผ๋ก ๊ฐ์ฃผํ๋ค.
string id = "myid";
string password = "mypassword";
Console.Write("์์ด๋๋ฅผ ์
๋ ฅํ์ธ์: ");
string inputId = Console.ReadLine();
Console.Write("๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์: ");
string inputPassword = Console.ReadLine();
if (inputId == id && inputPassword == password)
{
Console.WriteLine("๋ก๊ทธ์ธ ์ฑ๊ณต!");
}
else
{
Console.WriteLine("๋ก๊ทธ์ธ ์คํจ...");
}
๐ ์ํ๋ฒณ ํ๋ณ
๋ฌธ์ ํ๋๋ฅผ ์
๋ ฅ๋ฐ์ ์ํ๋ฒณ์ธ์ง ์๋์ง๋ฅผ ํ๋ณํ๋ค.
az, AZ ๋ฒ์์ ํด๋นํ๋ฉด ์ํ๋ฒณ์ด๋ค.
Console.Write("๋ฌธ์๋ฅผ ์
๋ ฅํ์ธ์: ");
char input = Console.ReadLine()[0];
if (input >= 'a' && input <= 'z' || input >= 'A' && input <= 'Z')
{
Console.WriteLine("์ํ๋ฒณ์
๋๋ค.");
}
else
{
Console.WriteLine("์ํ๋ฒณ์ด ์๋๋๋ค.");
}
๐ ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ 3 — ๋ฐ๋ณต๋ฌธ ๊ตฌ์กฐ (for, while, do-while, foreach, ์ค์ฒฉ ๋ฐ๋ณต, break/continue)
๐ for ๋ฌธ
for ๋ฌธ์ ๋ฐ๋ณต ํ์๊ฐ ๋ช
ํํ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
์ด๊ธฐ์, ์กฐ๊ฑด์, ์ฆ๊ฐ์์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋ค.
for (์ด๊ธฐ์; ์กฐ๊ฑด์; ์ฆ๊ฐ์)
{
// ์กฐ๊ฑด์์ด ์ฐธ์ธ ๋์ ์คํ๋๋ ์ฝ๋
}
์์
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
์ง์๋ง ์ถ๋ ฅํ๋ ์์ :
for (int i = 0; i < 10; i += 2)
{
Console.WriteLine(i);
}
๐ while ๋ฌธ
while ๋ฌธ์ ์กฐ๊ฑด์์ด ์ฐธ์ธ ๋์ ๊ณ์ ๋ฐ๋ณตํ๋ฉฐ, ๋ฐ๋ณต ํ์๊ฐ ๋ถ๋ช ํํ ๋ ์ ์ฉํ๋ค.
while (์กฐ๊ฑด์)
{
// ์กฐ๊ฑด์์ด ์ฐธ์ด๋ฉด ๋ฐ๋ณต ์คํ
}
์์
int count = 0;
while (count < 10)
{
Console.WriteLine("์ ์ ์ฒ์นํ์ต๋๋ค! ๋จ์ ์ ์: " + (10 - count - 1));
count++;
}
Console.WriteLine("์ถํํฉ๋๋ค! ๊ฒ์์์ ์น๋ฆฌํ์
จ์ต๋๋ค!");
๐ for vs while ๋น๊ต
int sum = 0;
// for ๋ฌธ
for (int i = 1; i <= 5; i++)
{
sum += i;
}
Console.WriteLine("1๋ถํฐ 5๊น์ง์ ํฉ (for): " + sum);
// while ๋ฌธ
sum = 0;
int j = 1;
while (j <= 5)
{
sum += j;
j++;
}
Console.WriteLine("1๋ถํฐ 5๊น์ง์ ํฉ (while): " + sum);
- for ๋ฌธ์ ๋ฐ๋ณต ์กฐ๊ฑด์ด ๊ณ ์ ๋ ๊ฒฝ์ฐ์ ์ ํฉํ๋ค.
- while ๋ฌธ์ ์ข ๋ฃ ์กฐ๊ฑด์ด ์ธ๋ถ์ ์ํด ๊ฒฐ์ ๋ ๋ ๋ ์ ์ฐํ๋ค.
๐ do-while ๋ฌธ
do-while ๋ฌธ์ ์กฐ๊ฑด์ ๊ฒ์ฌ ์ ์ ์ฝ๋ ๋ธ๋ก์ ์ต์ 1ํ ์คํํ๋ค.
do
{
// ์ต์ 1๋ฒ ์คํ
}
while (์กฐ๊ฑด์);
์์
int sum = 0;
int num;
do
{
Console.Write("์ซ์๋ฅผ ์
๋ ฅํ์ธ์ (0 ์
๋ ฅ ์ ์ข
๋ฃ): ");
num = int.Parse(Console.ReadLine());
sum += num;
} while (num != 0);
Console.WriteLine("ํฉ๊ณ: " + sum);
๐ foreach ๋ฌธ
foreach ๋ฌธ์ ๋ฐฐ์ด ๋๋ ์ปฌ๋ ์
์ ์์๋ฅผ ์ํํ ๋ ์ฌ์ฉ๋๋ค.
์ธ๋ฑ์ค๋ฅผ ์ง์ ๋ค๋ฃจ์ง ์์๋ ๋๋ฏ๋ก ๊ฐ๊ฒฐํ๊ณ ์์ ํ๋ค.
foreach (์๋ฃํ ๋ณ์ in ๋ฐฐ์ด)
{
// ๊ฐ ์์์ ๋ํด ์คํํ ์ฝ๋
}
์์
string[] inventory = { "๊ฒ", "๋ฐฉํจ", "ํ", "ํ์ด", "๋ฌผ์ฝ" };
foreach (string item in inventory)
{
Console.WriteLine(item);
}
๐ ์ค์ฒฉ ๋ฐ๋ณต๋ฌธ
๋ฐ๋ณต๋ฌธ ์์ ๋ ๋ค๋ฅธ ๋ฐ๋ณต๋ฌธ์ ๋ฃ์ ์ ์๋ค.
2์ฐจ์ ๋ฐ์ดํฐ๋ ๊ตฌ๊ตฌ๋จ์ฒ๋ผ ๋ณตํฉ์ ์ธ ํจํด์ ์ถ๋ ฅํ ๋ ์ฌ์ฉ๋๋ค.
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("i: {0}, j: {1}", i, j);
}
}
๊ตฌ๊ตฌ๋จ ์์:
for (int i = 2; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
Console.WriteLine(i + " x " + j + " = " + (i * j));
}
}
โน๏ธ break ์ continue
- break๋ ๋ฐ๋ณต๋ฌธ์ ์ฆ์ ์ข ๋ฃํ๋ค.
- continue๋ ํ์ฌ ๋ฐ๋ณต์ ๊ฑด๋๋ฐ๊ณ ๋ค์ ๋ฐ๋ณต์ผ๋ก ์งํํ๋ค.
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0)
{
continue; // 3์ ๋ฐฐ์๋ ์ถ๋ ฅํ์ง ์์
}
Console.WriteLine(i);
if (i == 7)
{
break; // 7 ์ดํ ๋ฐ๋ณต ์ข
๋ฃ
}
}
๋ฌดํ ๋ฃจํ ์ข ๋ฃ ์์:
int sum = 0;
while (true)
{
Console.Write("์ซ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int input = int.Parse(Console.ReadLine());
if (input == 0)
{
Console.WriteLine("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.");
break;
}
if (input < 0)
{
Console.WriteLine("์์๋ ๋ฌด์ํฉ๋๋ค.");
continue;
}
sum += input;
Console.WriteLine("ํ์ฌ๊น์ง์ ํฉ: " + sum);
}
Console.WriteLine("ํฉ๊ณ: " + sum);
๐ ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ 4 — ๋ฐ๋ณต๋ฌธ ์ฌํ ์ค์ต
๐ฎ ๊ฐ์๋ฐ์๋ณด ๊ฒ์
while ๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์ ํ์ฉํ ๋ฐ๋ณต ๊ตฌ์กฐ ์ค์ต ์์ ์ด๋ค.
ํ๋ ์ด์ด๊ฐ ์ปดํจํฐ์ ๊ฐ์ ์ ํ์ ํ ๋๊น์ง ๊ฒ์์ ๋ฐ๋ณตํ๋ค.
string[] choices = { "๊ฐ์", "๋ฐ์", "๋ณด" };
string playerChoice = "";
string computerChoice = choices[new Random().Next(0, 3)];
while (playerChoice != computerChoice)
{
Console.Write("๊ฐ์, ๋ฐ์, ๋ณด ์ค ํ๋๋ฅผ ์ ํํ์ธ์: ");
playerChoice = Console.ReadLine();
Console.WriteLine("์ปดํจํฐ: " + computerChoice);
if (playerChoice == computerChoice)
{
Console.WriteLine("๋น๊ฒผ์ต๋๋ค!");
}
else if ((playerChoice == "๊ฐ์" && computerChoice == "๋ณด") ||
(playerChoice == "๋ฐ์" && computerChoice == "๊ฐ์") ||
(playerChoice == "๋ณด" && computerChoice == "๋ฐ์"))
{
Console.WriteLine("ํ๋ ์ด์ด ์น๋ฆฌ!");
}
else
{
Console.WriteLine("์ปดํจํฐ ์น๋ฆฌ!");
}
}
- Random().Next(0, 3)์ผ๋ก ์ปดํจํฐ์ ์ ํ์ ๋ฌด์์๋ก ์ค์
- while ์กฐ๊ฑด์ ํตํด ๋ฐ๋ณต ์ฌ๋ถ๋ฅผ ์ ์ด
- if, else if, else ๊ตฌ์กฐ๋ฅผ ํ์ฉํ ์น๋ถ ํ๋จ
๐ฏ ์ซ์ ๋ง์ถ๊ธฐ ๊ฒ์
1๋ถํฐ 100 ์ฌ์ด์ ๋์๋ฅผ ์์ฑํ๊ณ , ํ๋ ์ด์ด๊ฐ ํด๋น ์ซ์๋ฅผ ๋ง์ถ ๋๊น์ง ๊ณ์ํด์ ์ถ์ธกํ๋๋ก ์ ๋ํ๋ค.
์
๋ ฅํ ์ซ์๊ฐ ์ ๋ต๋ณด๋ค ํฐ์ง, ์์์ง๋ฅผ ์๋ดํ๋ฉฐ ์๋ ํ์๋ ํจ๊ป ์ถ๋ ฅํ๋ค.
int targetNumber = new Random().Next(1, 101);
int guess = 0;
int count = 0;
Console.WriteLine("1๋ถํฐ 100 ์ฌ์ด์ ์ซ์๋ฅผ ๋ง์ถฐ๋ณด์ธ์.");
while (guess != targetNumber)
{
Console.Write("์ถ์ธกํ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์: ");
guess = int.Parse(Console.ReadLine());
count++;
if (guess < targetNumber)
{
Console.WriteLine("์ข ๋ ํฐ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์.");
}
else if (guess > targetNumber)
{
Console.WriteLine("์ข ๋ ์์ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์.");
}
else
{
Console.WriteLine("์ถํํฉ๋๋ค! ์ซ์๋ฅผ ๋ง์ถ์
จ์ต๋๋ค.");
Console.WriteLine("์๋ํ ํ์: " + count);
}
}
- Random().Next(1, 101)์ ํตํด 1~100 ์ฌ์ด์ ์ ๋ต ์์ฑ
- ์ฌ์ฉ์ ์ ๋ ฅ์ int.Parse(Console.ReadLine())๋ก ์ฒ๋ฆฌ
- ์๋ ํ์๋ count ๋ณ์๋ฅผ ํตํด ๋์