C#/수업 내용

[C#] 2021-08-20 오후 for문 연습 문제 1~10

JSH1 2021. 8. 20. 14:43

문제1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(i);
            }
        }
    }
}


문제2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("2 x {0}", i + 1);
            }
        }
    }
}


문제3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("줄넘기 횟수를 입력 하세요(1~10): ");
            int count = Convert.ToInt32(Console.ReadLine());

            if (count > 0 && count <= 10)
            {
                for (int i = 0; i < count; i++)
                {
                    Console.WriteLine("줄넘기를 {0}회 했습니다.", i + 1);
                }
            }
            
            else
            {
                Console.WriteLine("횟수 설정이 잘못되었습니다.");
            }
        }
    }
}


문제4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            int j = 0;

            for (int i = 0; i < 6; i++)
            {
                j = i + 1;
                
                if (0 == j % 2)
                {
                    Console.WriteLine("{0}: 짝", j);
                }
                
                else if (0 != j % 2)
                {
                    Console.WriteLine("{0}: 홀", j);
                }
            }
        }
    }
}


문제5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            int damage = 2;
            Console.WriteLine("영웅의 공격력{0}", damage);
            Console.Write("몇 회 공격하시겠습니까: ");
            int count = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("몬스터를 공격({0}) 했습니다.", damage);
            }
        }
    }
}


문제6

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine(10000 + i + 1);
            }
        }
    }
}


문제7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 12; i++)
            {
                if (0 != i % 2)
                {
                    Console.WriteLine(i);
                }
            }
        }
    }
}


문제8

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("{0}교시 {1}:30 ~", i + 1, i + 9);
            }
           
            for (int i = 4; i < 8; i++)
            {
                Console.WriteLine("{0}교시 {1}:30 ~", i + 1, i + 10);
            }
        }
    }
}


문제9

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("문자를 입력하세요: ");
            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine();

            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("{0}: {1}", (char)Convert.ToInt32(info.KeyChar + i), Convert.ToInt32(info.KeyChar) + i);
            }
        }
    }
}


문제10

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;
            int check = 0;

            for (int i = 0; i < 3; i++)
            {
                Console.Write("입력하세요: ");
                input = Console.ReadLine();

                if (i == 0 && input == "바나나")
                {
                    check++;
                    Console.WriteLine("맞았습니다.");
                }
                else if (i == 1 && input == "사과")
                {
                    check++;
                    Console.WriteLine("맞았습니다.");
                }
                else if (i == 2 && input == "딸기")
                {
                    check++;
                    Console.WriteLine("맞았습니다.");
                }
                else
                {
                    Console.WriteLine("틀렸습니다.");
                }

            }

            Console.WriteLine("총 맞은 갯수는 {0}개 입니다.", check);
        }
    }
}

'C# > 수업 내용' 카테고리의 다른 글

[C#] 2021-8-20 challenge 문제, Random 중복값 방지  (0) 2021.08.20
[C#] 2021-08-20 오후 for문 연습 문제 11~17, challenge  (0) 2021.08.20
[C#] for, foreach  (0) 2021.08.20
2021-08-20 오전  (0) 2021.08.20
[C#] Random  (0) 2021.08.20