์ปดํจํฐ๊ฐ ์ดํดํ ์ ์๋ ์ฝ๋๋ ์ด๋ ๋ฐ๋ณด๋ ๋ค ์งค ์ ์๋ค.
์ข์ ํ๋ก๊ทธ๋๋จธ๋ ์ฌ๋์ด ์ดํดํ ์ ์๋ ์ฝ๋๋ฅผ ์ง ๋ค. -๋งํด ํ์ธ๋ฌ
<์ ์ถ๋ ฅ ์์ >
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string str = ReadLine();
int number = Convert.ToInt32(str);
int numb = Int32.Parse(str);
int numbe = Convert.ToInt32(ReadLine());
int num = Int32.Parse(ReadLine());
WriteLine(number);
WriteLine(numb);
WriteLine(numbe);
WriteLine(num);
}
}
}
<๋ถ๊ธฐ๋ฌธ ์์ 1>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string input = ReadLine();
int number = Int32.Parse(input);
if (number < 0)
WriteLine("์์");
else if (number > 0)
WriteLine("์์");
else
WriteLine("0");
if (number % 2 == 0)
WriteLine("์ง์");
else
WriteLine("ํ์");
}
}
}
<๋ถ๊ธฐ๋ฌธ ์์ 2>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string input = ReadLine();
int number = Int32.Parse(input);
if (number > 0)
{
if (number % 2 == 0)
WriteLine("0๋ณด๋ค ํฐ ์ง์");
else
WriteLine("0๋ณด๋ค ํฐ ํ์");
}
else
{
WriteLine("0๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์");
}
}
}
}
<switch๋ฌธ ์์ >
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string input = ReadLine();
int number = Int32.Parse(input);
switch (number)
{
case 1:
WriteLine("ํ๋");
break;
case 2:
WriteLine("๋");
break;
case 3:
WriteLine("์
");
break;
default:
WriteLine("๋ด๊ฐ ์๋ ์ซ์๋ 1, 2, 3 ๋ฟ...");
break;
}
}
}
}
<๋ถ๊ธฐ๋ฌธ ์์ 2>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
Write("์์ผ์ ์
๋ ฅํ์ธ์.(์ผ,์,ํ,์,๋ชฉ,๊ธ,ํ ) : ");
string day = ReadLine();
switch (day)
{
case "์ผ":
WriteLine("Sunday");
break;
case "์":
WriteLine("Monday");
break;
case "ํ":
WriteLine("Tuesday");
break;
case "์":
WriteLine("Wednesday");
break;
case "๋ชฉ":
WriteLine("Thursday");
break;
case "๊ธ":
WriteLine("Friday");
break;
case "ํ ":
WriteLine("Saturday");
break;
default:
WriteLine($"{day}๋ ์์ผ์ด ์๋๋๋ค.");
break;
}
}
}
}
<switch๋ฌธ ์์ 2>
๋ช ๋ น ํ๋กฌํํธ๋ฅผ ํตํด ์ ๋ ฅ๋ฐ์ ๋ฌธ์์ด์ int ๋๋ float ํ์์ผ๋ก ๋ณํํ ํ,
๋ณํ๋ ๋ฐ์ดํฐ๋ฅผ object ํ์ ๊ฐ์ฒด์ ๋ฐ์ฑํ์ฌ ๋ฃ๋๋ค.
๊ทธ๋ฆฌ๊ณ switch๋ฌธ์์ ์ด ๊ฐ์ฒด ์์ ๋ฐ์ฑ๋ ๋ฐ์ดํฐ์ ํ์์ ๋ฐ๋ผ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
object obj = 123; //C# ์ปดํ์ผ๋ฌ๋ 123 ๋ฆฌํฐ๋ด์ ํ๊ฐํ์ฌ int ํ์์์ ์ ์ถํ๋ค.
//๊ทธ๋ฆฌ๊ณ obj ์์ ๋ฐ์ฑํด์ ๋ฃ๋๋ค.
string s = ReadLine();
if (int.TryParse(s, out int out_i))
obj = out_i;
else if (float.TryParse(s, out float out_f))
obj = out_f;
else
obj = s;
switch (obj)
{
case int i:
WriteLine($"{i}๋ int ํ์์
๋๋ค.");
break;
case float f:
WriteLine($"{f}๋ float ํ์์
๋๋ค.");
break;
default:
Write($"{obj}๋ ๋ชจ๋ฅด๋ ํ์์
๋๋ค.");
break;
}
}
}
}
< TryParse() vs Parse() >
C# ๊ธฐ๋ณธ ์ซ์ ํ์์ ๋ฌธ์์ด์ ์ซ์๋ก ๋ณํํ๋ Parse() ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
Parse()์ TryParse() ๋ฉ์๋๋ ๊ฐ์ ๊ธฐ๋ฅ์ ํ์ง๋ง
๋ณํ์ด ์คํจํ๋ ๊ฒฝ์ฐ์ ์ฐจ์ด์ ์ด ์๋ค.
Parse()๋ฉ์๋๋ ๋ณํ์ด ์คํจํ๋ฉด '์์ธ'๋ฅผ ๋์ง๋ค.
์์ธ๊ฐ ๋์ ธ์ง๋ฉด ํ๋ก๊ทธ๋จ์ ํ์ฌ ์ฝ๋์ ์คํ์ ๋ฉ์ถ๊ณ ํ๋ฆ์ ๋ค๋ฅธ ๊ณณ์ผ๋ก ์ด๋ํ๊ฒ ๋๋ค.
TryParse() ๋ฉ์๋๋ ๋ณํ์ ์ฑ๊ณต ์ฌ๋ถ๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ํ์ฌ์ ์ฝ๋ํ๋ฆ์ ์ ์งํ ์ ์๋ค.
TryParse()๊ฐ ๋ณํํ ๋ฐ์ดํฐ๋ ๋๋ฒ์งธ ๋งค๊ฐ๋ณ์์ ์ ์ฅ๋๋ค.
์ด ๋งค๊ฐ๋ณ์๋ ํน์ดํ๊ฒ๋ out์ด๋ผ๋ ํค์๋๋ก ์์๋์ด ์๋ค.
(out์ ์ถ๋ ฅ ์ ์ฉ ๋งค๊ฐ๋ณ์์ด๋ค.)
์ด๋ ๊ฒ ๋ ๋ฉ์๋๋ ๊ธฐ๋ฅ์ ๋์ผํ์ง๋ง ๋์ํ๋ ๋ฐฉ์์ด ๋ค๋ฅด๋ฏ๋ก
๊ฒฝํ์ ์์๊ฐ๋ฉด์ ์ํฉ์ ์ ์ ํ๊ฒ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
<when์ >
๋ฐ์ดํฐ ํ์์ ๋ฐ๋ผ ๋ถ๊ธฐํ๋ ๊ฒฝ์ฐ,
switch ๋ฌธ์ when ์ ์ ์ด์ฉํ์ฌ ์ถ๊ฐ์ ์ธ ์กฐ๊ฑด ๊ฒ์ฌ๋ฅผ ์ํํ ์ ์๋ค.
when์ case ์ ์ ๋ถ์ฌ ์ฌ์ฉํ๋ค.
if๋ฌธ๊ณผ ๋น์ทํ๋ฐ, case ์ ์ ๋ถ์ฌ์ด๋ค๊ณ ์๊ฐํ๋ฉด ์ฝ๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
object obj = null;
string s = ReadLine();
if (int.TryParse(s, out int out_i))
obj = out_i;
else if (float.TryParse(s, out float out_f))
obj = out_f;
else
obj = s;
switch(obj)
{
case int i:
WriteLine($"{i}๋ int ํ์์ด๋ค.");
break;
case float f when f >= 0 :
WriteLine($"{f}๋ ์์ float ํ์์ด๋ค.");
break;
case float f:
WriteLine($"{f}๋ ์์ float ํ์์ด๋ค.");
break;
default:
WriteLine($"{obj}๋ ๋ชจ๋ฅด๋ ํ์์ด๋ค.");
break;
}
}
}
}
<switch ์>
๋ฌธ๊ณผ ์์ ์ฐจ์ด๋ ์ด๋ ๋ค.
์์ ์ด๋ค ๊ณ์ฐ์ ํด์ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋๊ณ , ๋ฌธ์ ์ด๋ค ์ผ์ ํ๋ค.
์ด๋ค ์์ ์ ๋ถ๊ธฐ๊ฐ ํ์ํ ๋๋ switch๋ฌธ์ ์ฌ์ฉํ๋,
๋ถ๊ธฐ๋ฅผ ๊ฑฐ์ณ ๊ฐ์ ๋ด์ผ ํ๋ ๊ฒฝ์ฐ์๋ switch ์์ ์ฌ์ฉํ๋ฉด ๋ณด๋ค ์ฝ๊ธฐ ์ข์ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
~
์ ์ ์ฐธ๊ณ
C# Math.Floor ์ Math.Truncate ์ ์ฐจ์ด : ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ (naver.com)
<์์ >
- ์ฌ๋ฆผ : Math.Ceiling(double๊ฐ)
- ๋ด๋ฆผ : Math.Truncate(double๊ฐ)
- ๋ฐ์ฌ๋ฆผ : Math.Round(double๊ฐ [, ์๋ฆฟ์])
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
int input = Convert.ToInt32(ReadLine());
//1์ ์๋ฆฌ๋ฅผ ๋ฒ๋ฆผ. ์)92->90, 87->80
int score1 = (input / 10) * 10;
int score = (int)(Math.Truncate(input / 10.0) * 10);
string grade = "";
switch(score)
{
case 90:
grade = "A";
break;
case 80:
grade = "B";
break;
case 70:
grade = "C";
break;
case 60:
grade = "D";
break;
default:
grade = "E";
break;
}
WriteLine(grade);
}
}
}
<๋ฐ๋ณต๋ฌธ>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
int a = 10;
do
{
WriteLine(a);
a -= 2;
}
while (a > 0);
do
{
WriteLine(a);
a -= 2;
}
while (a > 0);
}
}
}
<์ค์ฒฉ for๋ฌธ์ ์ด์ฉํ ๋ณ์ฐ๊ธฐ>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
for(int j=0; j<=i; j++)
{
Write("*");
}
WriteLine();
}
}
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
*
**
***
****
*****
<foreach>
foreach๋ฌธ์ ๋ฐฐ์ด(๋๋ ์ปฌ๋ ์ )์ ์ํํ๋ฉฐ ๊ฐ ๋ฐ์ดํฐ ์์์ ์ฐจ๋ก๋๋ก ์ ๊ทผํ๋๋ก ํด์ค๋ค.
๋ฐฐ์ด(๋๋ ์ปฌ๋ ์ )์ ๋์ ๋๋ฌํ๋ฉด ์๋์ผ๋ก ๋ฐ๋ณต์ด ์ข ๋ฃ๋๋ค.
foreach(๋ฐ์ดํฐํ์ ๋ณ์๋ช in ๋ฐฐ์ด๋๋์ปฌ๋ ์ )
์ฝ๋๋๋์ฝ๋๋ธ๋ก
์ ํ์๊ณผ ๊ฐ์ด foreach๋ฌธ์ in ํค์๋์ ํจ๊ป ์ฌ์ฉ๋๋ค.
foreach ๋ฌธ์ด ํ๋ฒ ๋ฐ๋ณต์ ์ํํ ๋ ๋ง๋ค
๋ฐฐ์ด๋๋์ปฌ๋ ์ ์ ์์๋ฅผ ์ฐจ๋ก๋๋ก ์ํํ๋ฉด์ in ํค์๋ ์์ ์๋ ๋ณ์์ ๋ด์์ค๋ค.
๋ฐฐ์ด : ํ๋์ ๋ณ์๋ ๋จ ํ๋์ ๋ฐ์ดํฐ๋ง ๋ด์ ์ ์๋๋ฐ ๋ฐํด,
๋ฐฐ์ด์ ์ฌ๋ฌ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ์ ์๋ ์ฝ๋ ์์์ด๋ค.
(๋ณ์ ์ฌ๋ฌ ๊ฐ๋ฅผ ์ด์ด ๋ถ์ฌ๋์ ๊ฒ๊ณผ ๊ฐ๋ค๊ณ ํ ์ ์๋ค.)
(์ปฌ๋ ์ ๋ ์ฌ๋ฌ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ์ฝ๋ ์์๋ผ๋ ์ ์์ ๋ฐฐ์ด๊ณผ ๋น์ทํ์ง๋ง,
์ฌ์ฉํ๋ ๋ฐฉ์๊ณผ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ์ด์ ์ ๊ทผํ๋ ์๊ณ ๋ฆฌ์ฆ์ด ๋ค๋ฅด๋ค.)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
int[] arr = new int[] { 0, 1, 2, 3, 4};
foreach(int a in arr)
{
WriteLine(a);
}
}
}
}
์ถ๋ ฅ
0
1
2
3
4
<์ ํ๋ฌธ>
์ ํ๋ฌธ์ ํ๋ฆ์ ๋๊ณ ํ๋ก๊ทธ๋จ์ ์คํ ์์น๋ฅผ ์ํ๋ ๊ณณ์ผ๋ก ๋จ์จ์ ๋์ฝ์ํฌ ์ ์๋ค.
C#์์ ์ ๊ณตํ๋ ์ ํ๋ฌธ์ ๋ค์ 5๊ฐ์ง ์ด๋ค.
- break : ์คํ ์ค์ธ ๋ฐ๋ณต๋ฌธ์ด๋ switch ๋ฌธ์ ์คํ์ ์ค๋จ
- continue : continue๋ฌธ์ด ๋ฐ๋ณต๋ฌธ ์์ ์ฌ์ฉ๋๋ฉด, ํ์ฌ ์คํ์ค์ธ ๋ฐ๋ณต์ ๊ฑด๋ ๋ฐ๊ณ ๋ค์ ๋ฐ๋ณต์ผ๋ก ๋์ด๊ฐ๋ค.
- goto (์ฝ๋ ๊ฐ๋ ์ฑ์ ๊ทน์ ์ผ๋ก ๋ฎ์ถ๋ฏ๋ก, ์คํต)
- return (๋ฉ์๋ ๊ด๋ จ)
- throw (์์ธ์ฒ๋ฆฌ ๊ด๋ จ)
<break ์์ >
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
while (true)
{
Write("๊ณ์ํ ๊น์?(์/์๋์ค) : ");
string input = ReadLine();
if (input == "์๋์ค")
{
break;
}
}
}
}
}
<continue ์์ >
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
for(int i = 0; i < 10; i++)
{
if( i%2 == 0 )
continue;
WriteLine($"{i}๋ ํ์!");
}
}
}
}
1๋ ํ์!
3๋ ํ์!
5๋ ํ์!
7๋ ํ์!
9๋ ํ์!
<์ด๊ฒ์ด c#์ด๋ค ์ฑํฐ5 ์ฐ์ต๋ฌธ์ 3๋ฒ>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
int i = 0;
while(i<5)
{
int j = 0;
while(j<=i)
{
Write("*");
j++;
}
WriteLine();
i++;
}
}
}
}
<์ด๊ฒ์ด c#์ด๋ค ์ฑํฐ5 ์ฐ์ต๋ฌธ์ 3๋ฒ>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
int i = 5;
do
{
int j = 0;
while (j <i)
{
Write("*");
j++;
}
WriteLine();
i--;
}
while (i > 0);
}
}
}
<์ด๊ฒ์ด c#์ด๋ค ์ฑํฐ5 ์ฐ์ต๋ฌธ์ 4๋ฒ>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
WriteLine("๋ฐ๋ณต ํ์๋ฅผ ์
๋ ฅํ์ธ์ :");
string input = ReadLine();
int number = Convert.ToInt32(input);
if(number<=0)
{
WriteLine("0๋ณด๋ค ๊ฐ๊ฑฐ๋ ์์ ์ซ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.");
return;
}
for(int i=0; i< number; i++)
{
for(int j=0; j<=i; j++)
{
Write("*");
}
WriteLine();
}
}
}
}
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
16. ๊ฐ, ์ฐธ์กฐ์ ์ํ ๋งค๊ฐ๋ณ์ ์ ๋ฌ, ref์ out์ ์ฐจ์ด (0) | 2022.02.09 |
---|---|
15. ๋ฉ์๋๋ก ์ฝ๋ ๊ฐ์ถ๋ฆฌ๊ธฐ + ์ฌ๊ทํธ์ถ ํจ์์ง๊ธฐ(ํผ๋ณด๋์น ์์ ) (0) | 2022.02.09 |
13. null ๋ณํฉ ์ฐ์ฐ์ (0) | 2022.02.08 |
12. ๋นํธ ์ฐ์ฐ์ (0) | 2022.02.08 |
11. null ์กฐ๊ฑด๋ถ ์ฐ์ฐ์ (0) | 2022.02.08 |