}
switch(option) {
case 1 : result=number1 + number2; break;
case 2 : result=number1 - number2; break;
case 3 : result=number1 * number2; break;
case 4 : result=number1 / number2; break;
default: throw new InvalidOperationException (“Invalid Option”); }
return result;
}
class TestCalc {
static void Main(string[] args) {
try
{
CalcNumber obj = new CalcNumber();
obj.GetNumber(); obj.GetOption(); int result = pute(); Console.WriteLine(“The result is {0}” , result);
}
catch(Exception e) { Console.WriteLine(e.Message); } } }
比较与上例运行结果,是否完全相同: 从键盘输入8和2两个整数时, 选择3时的运行结果: