有没有简单的方法将类似于“5*4”的字符串转换为20?
java -cp meval.jar com.primalworld.math.MathEvaluator -cos(0)*(1+2)
java -cp meval.jar com.primalworld.math.MathEvaluator .05*200+3.01
我已经进行了一些谷歌搜索,并找到了一个 这里。虽然不完全符合你的需求,但或许会有所帮助。
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class ScriptDemo {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("a", 1);
engine.put("b", 2);
try {
String expression = "(a + b) > 2";
Object result = engine.eval(expression);
System.out.println(expression+" ? "+result);
} catch(ScriptException se) {
se.printStackTrace();
}
}
}
public static void main(String[] args) {
long a = 5;
long b = 4;
String theExpression = "a * b";
JexlEngine jexl = new JexlEngine();
Expression e = jexl.createExpression(theExpression);
JexlContext context = new MapContext();
context.set("a", a);
context.set("b", b);
Long result = (Long) e.evaluate(context);
System.out.println("The answer : " + result);
}
public static void main(String[] args) {
JexlEngine jexl = new JexlEngine();
Expression e = jexl.createExpression("5 * 4");
Integer result = (Integer) e.evaluate(null);
System.out.println("The answer : " + result);
}
你也可以使用BeanShell。
它实际上是一个Java源代码解释器,但最近我用它来评估一些包含变量的表达式(只有eval
方法使用了BeanShell,其余部分是为了准备输出):
import bsh.EvalError;
import bsh.Interpreter;
public class EVAL {
private static final String FORMAT = "%-5s | %-5s | %-5s | %s%n";
public static void main(String[] args) {
tabela("((a && b)||c)");
tabela("a ? (b || c) : (b && c)");
tabela("(a?1:0) + (b?1:0) + (c?1:0) >= 2");
}
private static void tabela(String expressao) {
System.out.printf(FORMAT, " a ", " b ", " c ", expressao);
System.out.printf(FORMAT, "-----", "-----", "-----", expressao.replaceAll(".", "-"));
try {
for (int i = 0; i < 8; i++) {
boolean a = (i & (1<<2)) != 0;
boolean b = (i & (1<<1)) != 0;
boolean c = (i & (1<<0)) != 0;
boolean r = eval(expressao, a, b, c);
System.out.printf(FORMAT, a, b, c, r);
}
} catch (EvalError ex) {
ex.printStackTrace();
}
System.out.println();
System.out.println();
}
private static boolean eval(String expressao, boolean a, boolean b, boolean c) throws EvalError {
Interpreter inter = new Interpreter();
inter.set("a", a);
inter.set("b", b);
inter.set("c", c);
Object resultado = inter.eval(expressao);
return (Boolean) resultado;
}
}
结果:
a | b | c | ((a && b)||c)
----- | ----- | ----- | -------------
false | false | false | false
false | false | true | true
false | true | false | false
false | true | true | true
true | false | false | false
true | false | true | true
true | true | false | true
true | true | true | true
a | b | c | a ? (b || c) : (b && c)
----- | ----- | ----- | -----------------------
false | false | false | false
false | false | true | false
false | true | false | false
false | true | true | true
true | false | false | false
true | false | true | true
true | true | false | true
true | true | true | true
a | b | c | (a?1:0) + (b?1:0) + (c?1:0) >= 2
----- | ----- | ----- | --------------------------------
false | false | false | false
false | false | true | false
false | true | false | false
false | true | true | true
true | false | false | false
true | false | true | true
true | true | false | true
true | true | true | true
我过去使用过JEval,发现它非常简单易用。这是一段代码片段:
import net.sourceforge.jeval.EvaluationException;
import net.sourceforge.jeval.Evaluator;
public class Main {
public static void main(String[] args) {
try {
System.out.println(new Evaluator().evaluate("5+4*3"));
}
catch (EvaluationException e) {
e.printStackTrace();
}
}
}