標籤:
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
java中運算子的解析和計算