public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
try {
int value=2;
SimpleBindings bind=new SimpleBindings();
SimpleBindings bind2=new SimpleBindings();
SimpleBindings bind3=new SimpleBindings();
bind.put("点数", value);
bind2.put("a", value);
bind2.put("b", value);
bind3.put("a1", value+1);
bind3.put("b1", value+2);
System.out.println(engine.eval("点数<50?50:Math.ceil(56/50)*50",bind));
System.out.println(engine.eval("demosum=function(a,b){return a+b}; demosum(a,b);",bind2)); // 定义方法,并调用支持参数绑定
System.out.println(engine.eval("(function(a1,b1){function demosub(a,b){ return a+b;}return demosub(a1,b1)})(a1,b1)",bind3)); // 使用js 立即执行函数
} catch (ScriptException e) {
e.printStackTrace();
}
}