注:此代码需在JDK1.6或更高版本下编译运行import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;public class Test { public static void main(String[] args) throws ScriptException {
String express = "1+2*(6+7)";
System.out.println(express + "=" + getValue(express));
} private static Object getValue(String express) throws ScriptException {
ScriptEngine se = new ScriptEngineManager().getEngineByName("ECMAScript");
return se.eval(express);
}
}