简体   繁体   English

用Java解决两个代数方程

[英]Solving two algebraic equations in java

I have two equations that need to be evaluated in java 我有两个方程需要在Java中评估

y=(x+1)*2-3
y=5

These equations are dynamic in nature 这些方程本质上是动态的

y= x*8x6-5*5
y= 3

y is known in these equations, I need to determine the value of x y在这些方程式中是已知的,我需要确定x的值

What is the best and easy way to write a program in java? 用Java编写程序的最佳简便方法是什么?

It seems that there are a couple of ways to go about this. 似乎有两种方法可以解决此问题。 My first thought (as always is overly complex and most likely not worth doing except for fun), is to use a create a grammar to parse out the order of operations, things that can evaluate to variables vs constants, etc. Then programatically solve the equations. 我的第一个想法(一如既往地过于复杂,除了有趣之外,很可能不值得做),是使用创建语法来解析操作顺序,可以求出变量与常量的结果等。然后以编程方式解决方程。 This however is not something easily done. 但是,这并非易事。 If this is for a compiler class, this might be worth looking at otherwise ignore it. 如果这是针对编译器类的,则可能值得一看,否则将其忽略。

My second thought was to just use brute force. 我的第二个想法是只使用蛮力。 Though you will need to figure out what to do with negative values of x. 虽然您将需要弄清楚如何处理x的负值。

public int solve(int y){
    int x=0;
    while(y>(x+1)*2-3)
        x++;
}

Some years later, hope this helps someone, to make this a lot simplier i will use the library exp4j ( https://www.objecthunter.net/exp4j/ ) and the IDE netbeans 8.2 ( https://netbeans.org/ ). 几年后,希望这对某人有帮助,使它变得更简单,我将使用库exp4j( https://www.objecthunter.net/exp4j/ )和IDE netbeans 8.2( https://netbeans.org/ ) 。 Create a frame like this 创建这样的框架

Later on the button add the code: 稍后在按钮上添加代码:

try { net.objecthunter.exp4j.Expression e = new ExpressionBuilder(txtFunc.getText()) .variables("x") .build() .setVariable("x", Double.parseDouble(txtVar.getText())); double result = e.evaluate(); txtRes.setText("" + result); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Revisa la función o la variable, Posibles errores de operación: División entre 0"); }

Note: this is intended to evaluate "x" So doing specifically that way is going to be little bit complicated, so we are going to do some math, if 5 is the value of the function evaluated then we isolate the value of "x". 注意:这是为了评估“ x”,所以这样做的方法将有些复杂,因此我们将做一些数学运算,如果5是所评估函数的值,则我们将“ x”的值隔离。 x=(y+1)/2 then re-evaluate, x=(5+1)/2=3, x=3 and with the code verify that this is actually the answer. x =(y + 1)/ 2,然后重新评估,x =(5 + 1)/ 2 = 3,x = 3,并通过代码验证这实际上是答案。
Comprobation 恭维

Same thing goes with the other function. 其他功能也一样。 (sorry for my technical english) (对不起我的技术英语)

If by saying the equations are "dynamic" we are to infer that you are trying to construct a program to solve for x in an arbitrary algebraic equation (or set of equations), that's well beyond the scope of this forum. 如果通过说方程是“动态的”,我们可以推断出您正在尝试构建一个程序来求解任意代数方程(或方程组)中的x,那么这已经超出了本论坛的讨论范围。 There are entire software packages designed to do things like that. 有完整的软件包设计用于执行此类操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM