Language/C#
메서드(Method)
ninanio3
2011. 11. 12. 00:08
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
-
-
/*
-
* 두 정수와 연산자를 입력받아 실행하는 메서드를 선언하고 호출하자.
-
* 메서드의 선언과 사용법을 익힌다.
-
* 준비하고 있는 상태를 선언이라 하고, 실행되고 있는 상태를 호출이라 한다.
-
* */
-
-
-
namespace Console_Test
-
{
-
public class Program
-
{
-
static void Main(string[] args)
-
{
-
Console.WriteLine("5칙연산입니다.");
-
Console.WriteLine("숫자 연산자 숫자순");
-
-
Console.WriteLine("첫 번째 수를 입력하세요(정수) : ");
-
int iNum1 = int.Parse(Console.ReadLine());
-
Console.WriteLine("+ - * / % 중에연산자를 입력하세요.");
-
string opp = Console.ReadLine();
-
Console.WriteLine("두 번째 수를 입력하세요(정수) : ");
-
int iNum2 = int.Parse(Console.ReadLine());
-
OperationCalculator opcal = new OperationCalculator();
-
int iNum3 = opcal.Calculator(iNum1, iNum2, opp);
-
-
Console.Write("입력한 수를 계산해보면");
-
Console.WriteLine("{0} {1} {2} = {3}", iNum1, opp, iNum2, iNum3);
-
}
-
public class OperationCalculator
-
{
-
int z;
-
public int Calculator(int x, int y, string opp)
-
{
-
switch (opp)
-
{
-
case "+": z = x + y;
-
break;
-
case "-": z = x - y;
-
break;
-
case "*": z = x * y;
-
break;
-
case "/": z = x / y;
-
break;
-
case "%": z = x % y;
-
break;
-
}
-
return z;
-
}
-
}
-
}
-
}
이 문제의 출처는 c#프로그래밍 기본기 익히기, 정보문화사, 25000, 조효은지음, 2010년 10월 4일 2쇄발행, ISBN 978-89-5674-452-0 이고, 풀이는 제가 직접 썼습니다. 저작권 침해가 됐다면 내리겠습니다.