250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • MySQL
  • ใ…ฃใ„ท
  • ์œ„์ž„
  • ์ปดํฌ์ง€์…˜

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

C#/์ด๊ฒƒ์ด C#์ด๋‹ค

14. ์ž…์ถœ๋ ฅ, ๋ถ„๊ธฐ๋ฌธ

2022. 2. 8. 22:25
728x90

์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋Š” ์–ด๋А ๋ฐ”๋ณด๋‚˜ ๋‹ค ์งค ์ˆ˜ ์žˆ๋‹ค.

์ข‹์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์‚ฌ๋žŒ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ง ๋‹ค. -๋งˆํ‹ด ํŒŒ์šธ๋Ÿฌ

<์ž…์ถœ๋ ฅ ์˜ˆ์ œ>

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();
            }
        }
    }
}
728x90

'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
    'C#/์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • 16. ๊ฐ’, ์ฐธ์กฐ์— ์˜ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ, ref์™€ out์˜ ์ฐจ์ด
    • 15. ๋ฉ”์†Œ๋“œ๋กœ ์ฝ”๋“œ ๊ฐ„์ถ”๋ฆฌ๊ธฐ + ์žฌ๊ท€ํ˜ธ์ถœ ํ•จ์ˆ˜์งœ๊ธฐ(ํ”ผ๋ณด๋‚˜์น˜ ์˜ˆ์ œ)
    • 13. null ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž
    • 12. ๋น„ํŠธ ์—ฐ์‚ฐ์ž
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”