我要提示用户输入一个号码,然后使用NCalc
来评估和解决公式作为字符串参数使用NCalc
string a = Console.ReadKey().ToString();
Expression e = new Expression("2 + [a] * 5");
object x = e.Evaluate();
Console.WriteLine("{0}", x.ToString());
Console.ReadKey();
这给了我的错误,我究竟做错了评估用户输入?
我要提示用户输入一个号码,然后使用NCalc
来评估和解决公式作为字符串参数使用NCalc
string a = Console.ReadKey().ToString();
Expression e = new Expression("2 + [a] * 5");
object x = e.Evaluate();
Console.WriteLine("{0}", x.ToString());
Console.ReadKey();
这给了我的错误,我究竟做错了评估用户输入?
你需要为你的文字值传递参数a
;
string a = Console.ReadKey().KeyChar.ToString(); // add KeyChar
Expression e = new Expression("2 + [a] * 5");
e.Parameters["a"] = a; // don't forget this line
object x = e.Evaluate();
Console.WriteLine("{0}", x.ToString());
Console.ReadKey();
一件事,
string a = Console.ReadKey().ToString();
// yields a string value: System.ConsoleKeyInfo
// and not the number you typed.
好多了使用
string a = Console.ReadLine()
// or
string a = Console.ReadKey().KeyChar.ToString(); // KeyChar
,它给出了一个例外“输入字符串格式不正确”。它指向这一行 - “object x = e.Evaluate();” –
@ Avi.S。再试一次,它是与'Console.ReadKey()的ToString();',它应该是'Console.ReadKey()KeyChar.ToString();' –
好得多,如果你使用'字符串= Console.ReadLine();' –
很抱歉,但你所说的 “表达” 是什么意思? –