C#封装四则运算示例

时间:2021-09-13 20:38:11
 1 using System;
 2 
 3 public class Operation
 4 {
 5     public static double GetResult(double numberA, double numberB, string operate)
 6     {
 7         double result = 0d;
 8         switch (operate)
 9         {
10             case "+":
11                 result = numberA + numberB;
12                 break;
13             case "-":
14                 result = numberA - numberB;
15                 break;
16             case "*":
17                 result = numberA * numberB;
18                 break;
19             case "/":
20                 result = numberA / numberB;
21                 break;
22         }
23         return result;
24     }
25 
26 
27     class Program
28     {
29         public static void Main(string[] args)
30         {
31             Console.Write("Please input numberA: ");
32             string strNumberA = Console.ReadLine();
33             Console.Write("Please select operate(+ - * /): ");
34             string strOperate = Console.ReadLine();
35             Console.Write("Please input numberB: ");
36             string strNumberB = Console.ReadLine();
37             string strResult = "";
38             strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA),Convert.ToDouble(strNumberB), strOperate));
39             Console.WriteLine("Result is :" + strResult);
40             Console.ReadLine();
41             Console.Write("Press any key to continue . . . ");
42             Console.ReadKey(true);
43         }
44     }
45 }

C#封装四则运算示例

C#封装四则运算示例