package com.LBH; import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException; import com.singularsys.jep.Jep;
import com.singularsys.jep.bigdecimal.BigDecComponents; public class JieXi { public static boolean getExpressionValue(String expression) {
Jep jep = new Jep(new BigDecComponents());
jep.addStandardConstants();
Object result = null;
try {
jep.parse(expression);
result = jep.evaluate();
} catch (Exception e) {
e.printStackTrace();
}
return (Boolean) result; } public static Object getMathValue(String str) throws ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("a", true);
engine.put("b", false);
engine.put("c", true);
Object result = engine.eval(str);
return result;
} public static void main(String[] args) throws Exception { String str = "(a||b)&&c";
System.out.println(str);
Object result = getMathValue(str);
System.out.println("关系运算符计算结果,类型:" + result.getClass().getName()
+ ",结果:" + result);
System.out.println("******************\n数学运算符"
+ getExpressionValue("(1+3)&&(0+10)"));
}
}
运算结果:
(a||b)&&c
关系运算符计算结果,类型:java.lang.Boolean,结果:true
******************
数学运算符true
参考:http://blog.sina.com.cn/s/blog_acdc06250101p5dh.html