2013-05-07 60 views
0

我有一個字符串格式的公式,例如我必須用Java解決的「45 + 5 * 4-6」。求解字符串格式的方程

有什麼方法可以解決字符串格式的方程嗎?

+0

你可以使用'ScriptEngine'在[這個答案](看到http://stackoverflow.com/a/7441804/418556 )。 – 2013-05-07 07:35:25

+0

甚至這[回答](http://stackoverflow.com/a/3423360/2024761)。 – SudoRahul 2013-05-07 07:37:05

回答

1

檢查Beanshell

像這樣的東西應該工作 -

Interpreter ip = new Interpreter(); 
ip.eval("res = 45+5*4-6"); 
System.out.print(ip.get("res")); 
+0

感謝bhushan..it的工作.. – johntom 2013-05-07 09:07:00