我使用的是singularsys的“数学表达式解析器”库。下面是它的文档:Official tutorials和Documentation
我想要做的是:
Jep jep = new Jep();
jep.addVariable("ARR", new int[]{1,2,3});
jep.parse("ARR == 3");
Object result = jep.evaluate();
boolean ok = false;
if(result != null)ok = Boolean.valueOf(result.toString());
System.out.println(ok);我设置了一个名为ARR的变量,它包含数字1、2和3。我需要它来检查数组是否包含数字3,并返回"true“。
你知道要使用什么运算符或函数吗,或者这是否可能?我在文档中找不到任何东西,但我有一种感觉,不知何故这是可行的。
发布于 2015-12-17 20:36:33
我最终构建了一个自定义函数:
public static class JepContains extends BinaryFunction
{
public Object eval(Object arg1, Object arg2)
{
int[] arr = (int[])arg1;
int target = ((Double)arg2).intValue();
for(double i : arr)
if(i==target)
return true;
return (Object)false;
}
}下面是我如何使用它的:
jep.addFunction("contains",new JepContains());
jep.addVariable("ARR", new int[]{1,2,3});
jep.parse("contains(ARR, 2)"); // returns "true"https://stackoverflow.com/questions/34333764
复制相似问题