相关疑难解决方法(0)

在Java中评估数学表达式的方法

在我的一个项目中,我想添加一个功能,例如用户可以在公式中提供

sin (x + pi)/2 + 1
Run Code Online (Sandbox Code Playgroud)

我在我的Java应用程序中使用它

/**
 * The formula provided by the user
 */
private String formula; // = "sin (x + pi)/2 + 1"

/*
 * Evaluates the formula and computes the result by using the
 * given value for x
 */
public double calc(double x) {
    Formula f = new Formula(formula);
    f.setVar("x", x);
    return f.calc();
    // or something similar
}
Run Code Online (Sandbox Code Playgroud)

我如何评估数学表达式?

java math formula

49
推荐指数
3
解决办法
7万
查看次数

在if语句中使用字符串作为变量子句

private String boundaries = "(x > 750) & (x < 900)";
if (boundaries) {
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)

我希望能够在if子句中使用字符串作为变量,然后独立更改字符串,而无需编辑代码本身.示例的想法不起作用,我想知道它是否有错误或其他方式不使用字符串.

谢谢.

java

4
推荐指数
1
解决办法
91
查看次数

标签 统计

java ×2

formula ×1

math ×1