比如计算下面字符串数学表达式的值String str = "1+2*(3+6)-5/2";
2 回答
神不在的星期二
TA贡献1963条经验 获得超6个赞
OgnlContext oc = new OgnlContext();
Object object = Ognl.getValue("1+2*(3+6)-5/2", oc, oc.getRoot());
System.out.println(object.toString());
慕的地8271018
TA贡献1796条经验 获得超4个赞
public
class
Test {
public
static
void
main(String[] args)
throws
ScriptException {
ScriptEngineManager manager =
new
ScriptEngineManager();
ScriptEngine se = manager.getEngineByName(
"js"
);
String str =
"1+2*(3+6)-5/2"
;
Double result =(Double) se.eval(str);
System.out.println(result);
}
}
添加回答
举报
0/150
提交
取消