Packagecom. LBH; ImportJavax.script.ScriptEngine;ImportJavax.script.ScriptEngineManager;Importjavax.script.ScriptException;ImportCom.singularsys.jep.Jep;Importcom.singularsys.jep.bigdecimal.BigDecComponents; Public classJiexi { Public Static BooleanGetexpressionvalue (String expression) {Jep Jep=NewJEP (Newbigdeccomponents ()); Jep.addstandardconstants (); Object result=NULL; Try{jep.parse (expression); Result=jep.evaluate (); } Catch(Exception e) {e.printstacktrace (); } return(Boolean) result; } Public StaticObject getmathvalue (String str)throwsscriptexception{Scriptenginemanager Manager=NewScriptenginemanager (); ScriptEngine Engine= Manager.getenginebyname ("JS"); Engine.put ("A",true); Engine.put ("B",false); Engine.put ("C",true); Object result=engine.eval (str); returnresult; } Public Static voidMain (string[] args)throwsException {String str= "(a| | b) &&c "; System.out.println (str); Object result=Getmathvalue (str); System.out.println ("Relational operators evaluate results, type:" +Result.getclass (). GetName ()+ ", Result:" +result); System.out.println ("******************\n math Operator" + getexpressionvalue ("(1+3) && (0+10)")); }}
Result of Operation:
(a| | b) &&c
Relational operator calculation result, type: Java.lang.Boolean, Result: true
******************
Mathematical operator True
Reference: http://blog.sina.com.cn/s/blog_acdc06250101p5dh.html
Parsing and computing of operators in Java