将表达式替换成js使用的文本格式。然后带入eval函数。

public class JieXi
{
  public static void main(String[] args) throws Exception {
         String str = "(a or b) and c";
         str = str.replaceAll("or", "||");
         str = str.replaceAll("and", "&&");
         System.out.println(str);
         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);
         System.out.println("结果类型:" + result.getClass().getName() + ",计算结果:" + result);
  
     }
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
  • 2022-02-07
  • 2021-07-21
  • 2022-02-02
  • 2021-06-19
猜你喜欢
  • 2021-05-21
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2021-07-30
相关资源
相似解决方案