可能重复:
.NET 中是否有字符串数学评估器?
有没有办法在不检查"+", "-", "*", "/"
然后传递值的情况下转换操作员代码?
using System;
namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
string operator = "+";
int inputOne = 2;
int inputTwo = 5;
Console.WriteLine(Result(inputOne, inputTwo, operator));
Console.ReadLine();
}
public static string Result(int one, int two, string computeOperator)
{
if (computeOperator == "+")
{
return (one + two).ToString();
}
if (computeOperator == "-")
{
return (one - two).ToString();
}
//and so on...
return "0";
}
}
}