[英]Does anyone know how to use Expression in Android Studio?
I'm making a calculator in Android Studio and am facing a problem. 我正在Android Studio中制作计算器,但遇到了问题。
Here is my code: 这是我的代码:
String input = getinput();
try {
if (!isEmpty() && !stateError) {
if (input.contains("x")){
input.replaceAll("x","*");
}
Expression expression = new ExpressionBuilder(input).build();
}
} catch (Exception e) { }
When I type Expression
, it shows the error: 当我输入
Expression
,它显示错误:
"Cannot resolve symbol 'Expression'."
“无法解析符号'表达式'。”
Is there any solution to solve this problem? 有解决此问题的解决方案吗?
I don't know if you have found this or not, but there is this site that I found that seems to be the API for Expression
and ExpressionBuilder
called exp4j. 我不知道您是否找到了这个,但是我发现这个站点似乎是
Expression
和ExpressionBuilder
的API,称为exp4j。
If that is in fact the API, then this is the download link for it. 如果实际上是API,则这是它的下载链接 。
Another way to evaluate an equation from a String
in Java is to use the ScriptEngine interface along with the ScriptEngineManager class. 从Java中的
String
求值方程式的另一种方法是将ScriptEngine接口与ScriptEngineManager类一起使用。 You can use it like the following: 您可以像以下方式使用它:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");
try {
String result = engine.eval(stringEquation).toString();
System.out.println("Result: "+result);
}
catch (ScriptException se) {
// Handle error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.