본문 바로가기

   
Programming/C#

if 문 과제

반응형

using System;

 

namespace Csharp

{

    //If1_BigNumber.cs

    class If1_BigNumber

    {

        public static void Main(string[] args)

        {

            /*----------------------------------------------------if 쓰시오

                       [문제1]----------------------------------------------------------

                       요구사항] 숫자를 2 입력받아서 큰수를 출력하시오.

                       입력]

                        숫자 1 : 10

                        숫자 2 : 5

                       출력]

                        입력하신 10() 5 수는 10 입니다.

                       ---------------------------------------------------------------*/

            int num1 = new int(), num2 = new int(), result = new int();

 

            Console.Write("숫자 1 : ");

            num1 = int.Parse(Console.ReadLine());

            Console.Write("숫자 2 : ");

            num2 = int.Parse(Console.ReadLine());

 

            if (num1 >= num2)

            {

                result = num1;

            }

            else if (num1 < num2)

            {

                result = num2;

            }

            Console.WriteLine("입력하신 {0}() {1} 수는 {2} 입니다.", num1, num2, result);

 

            Console.ReadLine(); //프로그램 종료 방지

        }

    }

}

 


 

using System;

 

namespace Csharp

{

    //If2_TestPoint.cs

    class If2_TestPoint

    {

        public static void Main(string[] args)

        {

            /*

                       [문제2]----------------------------------------------------------

                       요구사항] 점수를 입력받아 성적을 출력하시오

 

                       입력 ] 점수입력 : 95

                       출력 ] 입력하신 95점은 'A'입니다.

                       조건 ] 90 ~ 100 : A

                                 80 ~ 89      : B

                                 70 ~ 79      : C

                                 60 ~ 69      : D

                                 0 ~ 59       : F

                       -----------------------------------------------------------------

                       */

            int point;

            Console.Write("점수입력 : ");

            point = int.Parse(Console.ReadLine());

 

            Console.WriteLine("\r\n\t 입력하신 {0} 점은 {1}학점 입니다.\r\n", point, PointResult(point));

 

            Console.ReadLine(); //프로그램 종료 방지

 

 

        }

        public static string PointResult(int point)

        {

            string result = "";

            if (point > 0 && point < 60)

            {

                result = "F";

            }

            else if (point >= 60 && point <= 69)

            {

                result = "D";

            }

            else if (point >= 70 && point <= 79)

            {

                result = "C";

            }

            else if (point >= 80 && point <= 89)

            {

                result = "B";

            }

            else if (point >= 90 && point <= 100)

            {

                result = "A";

            }

            return result;

        }

    }

}



 

using System;

 

namespace Csharp

{

    //If3_MiniCalc.cs

    class If3_MiniCalc

    {

        public static void Main(string[] args)

        {

            /*

                       [문제3]----------------------------------------------------------

                       요구사항] 숫자 2개와 연산자 1개를 입력받아서 식과 결과를 출력

 

                       입력] 첫번째 : 5

                                두번째 : 3

                                연산자 : +

 

                       출력] 5 + 3 = 8

                       조건] 연산자는 + ,- ,* ,/ ,%

                       -----------------------------------------------------------------*/

 

            double firstInput, secondInput;

            double result = 0.0;

            char calculator;

            Console.Write("첫번째 : ");

            firstInput = int.Parse(Console.ReadLine());

            Console.Write("두번째 : ");

            secondInput = int.Parse(Console.ReadLine());

            Console.Write("연산자 : ");

            calculator = char.Parse(Console.ReadLine());

 

            if (calculator == '+')

            {

                result = (double)(firstInput + secondInput);

            }

            else if (calculator == '-')

            {

                result = (double)(firstInput - secondInput);

            }

            else if (calculator == '*')

            {

                result = (double)(firstInput * secondInput);

            }

            else if (calculator == '/')

            {

                result = (double)(firstInput / secondInput);

            }

            else if (calculator == '%')

            {

                result = (double)(firstInput % secondInput);

            }

 

            Console.WriteLine("{0} {1} {2} = {3:F2}", firstInput, calculator, secondInput, result);

 

 

 

            Console.ReadLine(); //프로그램 종료 방지

 

        }

    }

}


using System;

 

namespace Csharp

{

    //If4_Family.cs

    class If4_Family

    {

        public static void Main(string[] args)

        {

            /*

                       [문제4]----------------------------------------------------------

                       요구사항] f, m, b, s 문자를 입력받아 아래와 같이 출력.

 

                       입력] f

                       출력] Father

                       조건] 입력할 문자 , 소문자 관계없이 입력 가능하게..

                                f, F -> Father m, M -> Mother b, B -> Brother s,S -> Sister

                       -----------------------------------------------------------------

                       */

            a:

            string firstInput;

            Console.Write("입력할 문자 : ");

            firstInput = Console.ReadLine();

 

            char ini = new char();

            if (firstInput.Length == 1)

            {

                ini = char.Parse(firstInput);

                Console.WriteLine("출력 : {0}", FamilyInitial(ini));

            }

            else

            {

                Console.WriteLine("문자 한개만 입력하세요");

                goto a;

            }

            Console.ReadLine(); //프로그램 종료 방지

 

 

 

        }

        public static string FamilyInitial(char ini)

        {

 

            string result = "";

            if ((ini == 'f') || (ini == 'F'))

            {

                result = "Father";

            }

            else if ((ini == 'm') || (ini == 'M'))

            {

                result = "Mother";

            }

            else if ((ini == 's') || (ini == 'S'))

            {

                result = "Sister";

            }

            else if ((ini == 'b') || (ini == 'B'))

            {

                result = "Brother";

            }

            else

            {

                result = "s, f, b, m 하나를 입력하세요";

            }

 

            return result;

        }

    }

}


 

using System;

 

namespace Csharp

{

    //If5_ChangeWord.cs

    class If5_ChangeWord

    {

        public static void Main(string[] args)

        {

            /*

                       [문제5]----------------------------------------------------------

                       요구사항] 영문자 1개를 입력받아서 ,소문자를 변환하시오.(문자코드값)

                       입력 ] a

                       출력 ] A

                       입력 ] E

                       출력 ] e

                       -----------------------------------------------------------------

                       */

            a:

            string firstInput;

            Console.Write("입력문자 : ");

            firstInput = Console.ReadLine();

 

            char ini = new char();

            if (firstInput.Length == 1)

            {

                ini = char.Parse(firstInput);

                Console.WriteLine("출력문자 : {0}", WordChanger(ini));

            }

            else

            {

                Console.Clear();

                Console.WriteLine("문자 한개만 입력하세요");

                goto a;

            }

            Console.ReadLine(); //프로그램 종료 방지

 

 

 

        }

        public static char WordChanger(char ini)

        {

 

            char result = new char();

            if ((ini >= 'a') && (ini <= 'z'))

            {

                result = (char)(ini - 32);

            }

            else if ((ini >= 'A') && (ini <= 'Z'))

            {

                result = (char)(ini + 32);

            }

            else

            {

                result = '?';

            }

 

            return result;

        }

    }

}

 

 

 

using System;

 

namespace Csharp

{

    //If6_Sum3Int.cs

    class If6_Sum3Int

    {

        public static void Main(string[] args)

        {

            /*---------------------------------------------------------------

                       [문제6]----------------------------------------------------------*********

                       요구사항] 3자리 정수를 입력 받아서 각각의 자릿수의 합을 구하기.

                       입력 ] 256

                       출력 ] 2 + 5 + 6 = 13

                       -----------------------------------------------------------------

                       ---------------------------------------------------------------*/

            a:

            string readline = "";

            Console.Write("┌────────────────────┐\r\n");

            Console.Write("│                    │\r\n");

            Console.Write("└────────────────────┘\r\n");

            Console.SetCursorPosition(5, 1);

            Console.Write("\t3자리숫자 입력 : ");

            readline = Console.ReadLine();

            Console.Write("├────────────────────┤\r\n");

 

            if (readline.Length == 3)

            {

                Sum3Int(readline);

            }

            else

            {

                Console.Clear();

                Console.Write("┌────────────────────┐\r\n");

                Console.Write("│   3자리 숫자만 입력하세요!     │\r\n");

                Console.Write("└────────────────────┘\r\n");

                Console.ReadLine();

                Console.Clear();

                goto a;

 

            }

            Console.ReadLine(); //프로그램 종료 방지

 

        }

        public static void Sum3Int(string readline)

        {

            int num1 = new int(), result = new int();

            int third, second, first;

 

            num1 = int.Parse(readline);

 

            third = num1 / 100;

            second = (num1 % 100) / 10;

            first = num1 % 10;

 

            result = third + second + first;

            Console.Write("│                    │\r\n");

            Console.Write("└────────────────────┘\r\n");

            Console.SetCursorPosition(5, 3);

            Console.WriteLine("\t결과! {0} + {1} + {2} = {3}\r\n\r\n", third, second, first, result);

            Console.ReadLine();

        }

    }

}


 
 
 using System;

 

namespace Csharp

{

    //If7_YoonYear

    class If7_YoonYear

    {

        public static void Main(string[] args)

        {

            /*

                       [문제7]----------------------------------------------------------

                       요구사항] 년도를 입력받아서, "평년","윤년"인지 구하시오.

                       입력] 년도 : 2012

                       출력] 입력하신 2012년은 "윤년"입니다.

                       조건] a. 년도를 4 나누어서 떨어지면 b 검사

                                       년도를 4 나누어서 떨어지지 않으면 "평년"

                                b. 년도를 100으로 나누어서 떨어지면 c 검사

                                   년도를 100으로 나누어서 떨어지지 않으면 "윤년"

                                c. 년도를 400으로 나누어서 떨어지면 "윤년"

                                   년도를 400으로 나누어서 떨어지지 않으면 "평년"

                       -----------------------------------------------------------------

                       ---------------------------------------------------------------*/

            string input = "";

            Console.Write("년도 입력 : ");

            input = Console.ReadLine();

            if (input.Length != 4)

            {

                Console.WriteLine("4자리수를 입력하세요");

            }

            else

            {

                Console.WriteLine("{0}년도는 {1} 입니다.", input, YoonYear(input));

            }

            Console.ReadLine(); //프로그램 종료 방지

        }

        public static string YoonYear(string input)

        {

            string result = "";

 

            if ((int.Parse(input) % 4) == 0)

            {

                if ((int.Parse(input) % 100) == 0)

                {

                    if ((int.Parse(input) % 400) == 0)

                    {

                        result = "윤년";

                    }

                    else

                    {

                        result = "평년";

                    }

                }

                else

                {

                    result = "윤년";

                }

            }

            else

            {

                result = "평년";

            }

            return result;

        }

    }

}




using System;

 

namespace Csharp

{

    //Sw1_TestPoint.cs

    class Sw1_TestPoint

    {

        public static void Main(string[] args)

        {

 

 

            /*----------------------------------------------switch문만 쓰시오

                       [문제1]----------------------------------------------------------

                       요구사항] 점수를 입력받아 성적을 출력하시오

 

                       입력 ] 점수입력 : 95

                       출력 ] 입력하신 95점은 'A'입니다.

                       조건 ] 90 ~ 100 : A

                                 80 ~ 89      : B

                                 70 ~ 79      : C

                                 60 ~ 69      : D

                                 0 ~ 59       : F

                       -----------------------------------------------------------------

                       */

 

            string result = "";

            Console.Write("점수입력 : ");

            int input = int.Parse(Console.ReadLine());

            input = input / 10;

 

            switch (input)

            {

                case 10:

                case 9: result = "A"; break;

 

                case 8: result = "B"; break;

                case 7: result = "C"; break;

                case 6: result = "D"; break;

 

                case 5:

                case 4:

                case 3:

                case 2:

                case 1:

                case 0: result = "F"; break;

                default: result = "올바른 값을 입력하세요"; break;

            }

 

            Console.WriteLine("{0}", result);

 

            Console.ReadLine(); //프로그램 종료 방지

 

 

        }

    }

}


 

using System;

 

namespace Csharp

{

    //Sw2_MonthDay.cs

    class Sw2_MonthDay

    {

        public static void Main(string[] args)

        {

            /*

                       [문제2]----------------------------------------------------------

                       요구사항] 월을 입력받아서 마지막 일이 몇일인지?

                       입력 ] 5

                       출력 ] 입력하신 5월은 31일까지 있습니다.  (2월은 28일로)

                       -----------------------------------------------------------------

                       */

            Console.Write("입력월 : ");

            int input = int.Parse(Console.ReadLine());

            Console.WriteLine("{0}", MonthDay(input));

 

            Console.ReadLine(); //프로그램 종료 방지

 

        }

        public static string MonthDay(int input)

        {

            string result = "";

            switch (input)

            {

                case 1:

                case 3:

                case 5:

                case 7:

                case 8:

                case 10:

                case 12: result = "31 입니다."; break;

 

                case 4:

                case 6:

                case 9:

                case 11: result = "30 입니다."; break;

 

                case 2: result = "28 입니다."; break;

                default: result = "1~12사이의 숫자를 입력하세요!"; break;

            }

            return result;

        }

    }

}


 


반응형