public static void main(String[] args) {
Map<String,Object> map =new HashMap<>();
map.put("SJL",6);
String formula="(SJL>100?100: SJL>50?80: SJL>0?30:0)*0.3";
ScriptEngine engine;
ScriptEngineManager manager = new ScriptEngineManager();
engine = manager.getEngineByName("JavaScript");
try {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String k = entry.getKey();
Object v = entry.getValue();
engine.put(k, v);
}
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(engine.eval(formula)));
} catch (ScriptException e) {
e.printStackTrace();
if (e.getMessage().indexOf("is not defined")!=-1)
{
Pattern p= Pattern.compile("\"(.*?)\"");
Matcher m = p.matcher(e.getMessage());
StringBuffer res =new StringBuffer();
while (m.find()) {
res.append(m.group().trim().replace("\"",""));
}
System.out.println("参数:"+res+" 未赋值");
}
}
}