通过将变量传递给 Math.pow( ) 使用 beanshell 评估 java 中表达式的值
evaluate the value of a expression in java using beanshell by passing variable to Math.pow( )
我试图直接计算 java 中的表达式,虽然它是一个简单的表达式,但使用 beanshell
对我来说似乎很难
无论如何,这是我的问题:
i 可以直接使用 in java
计算指数值
double c = Math.pow(2,3);
或其他方式
int a=2,b=3;
double c = Math.pow(a,b);
在 Beanshell 中,我正在尝试做同样的事情,它有效:
import bsh.Interpreter;
Interpreter interpreter = new Interpreter();
int a1=2, b1=3;
String equation = "Math.pow(2,3)";
Object checkIt = interpreter.eval(equation);
System.out.println(checkIt.toString);
但是这些行不起作用:
String equation = "Math.pow(a1,b1)";
Object checkIt = interpreter.eval(equation);
System.out.println(checkIt.toString);
更新:我的代码显示了错误消息
源文件:内联评估:``Math.pow(finalStr,2);'':未定义的参数:
BeanShell 脚本无法访问 Java 局部变量。该脚本只能看到使用任何 set()
方法提供给 BeanShell Interpreter
的值:
import bsh.Interpreter;
Interpreter interpreter = new Interpreter();
interpreter.set("a1", 2);
interpreter.set("b1", 3);
String equation = "Math.pow(a1,b1)";
Object checkIt = interpreter.eval(equation);
System.out.println(checkIt);
javadoc甚至有这样的例子。
我试图直接计算 java 中的表达式,虽然它是一个简单的表达式,但使用 beanshell
对我来说似乎很难无论如何,这是我的问题:
i 可以直接使用 in java
计算指数值double c = Math.pow(2,3);
或其他方式
int a=2,b=3;
double c = Math.pow(a,b);
在 Beanshell 中,我正在尝试做同样的事情,它有效:
import bsh.Interpreter;
Interpreter interpreter = new Interpreter();
int a1=2, b1=3;
String equation = "Math.pow(2,3)";
Object checkIt = interpreter.eval(equation);
System.out.println(checkIt.toString);
但是这些行不起作用:
String equation = "Math.pow(a1,b1)";
Object checkIt = interpreter.eval(equation);
System.out.println(checkIt.toString);
更新:我的代码显示了错误消息 源文件:内联评估:``Math.pow(finalStr,2);'':未定义的参数:
BeanShell 脚本无法访问 Java 局部变量。该脚本只能看到使用任何 set()
方法提供给 BeanShell Interpreter
的值:
import bsh.Interpreter;
Interpreter interpreter = new Interpreter();
interpreter.set("a1", 2);
interpreter.set("b1", 3);
String equation = "Math.pow(a1,b1)";
Object checkIt = interpreter.eval(equation);
System.out.println(checkIt);
javadoc甚至有这样的例子。