81,094
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws ScriptException {
Map<String, Object> map = new HashMap();
map.put("x", 54.1);
map.put("y", 545563.45);
map.put("z", 75555.53);
map.put("d", 25555.64);
String exp = "if([[x]]>5,\"上升\"+([[z]]-[[d]]+[[y]]/2+[[y]]),\"下降\"+(-([[y]]-[[x]])))";
exp=exp.replaceAll("if\\((.*?),(.*?),(.*?)\\)","\\(\\($1\\)?$2:$3\\)");
exp = exp.replaceAll("\\[", "").replaceAll("\\]", "");
System.out.println(exp);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine scriptEngine = manager.getEngineByName("js");
for (int k = 0; k < map.keySet().size(); k++)
{
Object key = map.keySet().toArray()[k];
Object value = map.get(key);
scriptEngine.put(key.toString(), value);
}
Object result = scriptEngine.eval(exp);
System.out.println("结果:" + result );
}