[英]Convert String to math equation?
I'm trying to figure out how to parse an input string that contains a math equation given by the user like 2x^2+3x+6
to a double that operates the String with its variables that I will define later on with a for loop.我试图弄清楚如何将包含用户给出的数学方程(如
2x^2+3x+6
的输入字符串解析为一个双精度值,该字符串使用它的变量来操作字符串,稍后我将使用 for 循环定义这些变量.
Basically,基本上,
for(x=0;x<=100;x++){
String equation = "2x^2+3x+6";
double y = equation;
System.out.println("Your point is x: " + x +" | Y: " + y);
}
I found this old post but the BeanSheel Library only allowed me to use actual numbers without parameters like "x"
.我找到了这篇旧帖子,但 BeanSheel 库只允许我使用没有像
"x"
这样的参数的实际数字。
So what I'm trying to achieve is a program that will draw a function given by the user.所以我想要实现的是一个程序,它将绘制用户给出的函数。 But for the drawing part I'm fine
但对于绘图部分我很好
You could replace every occurrence of x
within the equation string with the string representation of x
's value;您可以用
x
值的字符串表示形式替换方程字符串中出现的每个x
; then you could use the post you linked to to evaluate it.然后你可以使用你链接到的帖子来评估它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.