你的用委托写的有点麻烦,看看我的,一目了然- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class 计算器
- {
- static void main(string[] ager) {
- CCount count = new CCount();
- Console.Write("请输入第一个数:\n");
- int a = Convert.ToInt32(Console.ReadLine());
- Console.Write("请输入第二个数:\n");
- int b = Convert.ToInt32(Console.ReadLine());
- Console.Write("请输入运算符符号(+-*/):\n");
- string type = Console.ReadLine();
- Console.Write(count.Sum(a,b,type)+"\n");
- Console.ReadLine();
- }
- }
- public class CCount {
- public int Sum(int a,int b,string type) {
- switch (type)
- {
- case "+":
- return a + b;
- break;
- case "-":
- return a - b;break;
- case "*":
- return a * b;break;
- case "/":
- return a / b;break;
- default:
- return 0; break;
- }
- }
- }
- }
复制代码 |