Java for Android中的二次方程式,无法发现我的错误

时间:2014-05-30 14:01:35

标签: java android math equation quadratic

我目前正在制作一个应用程序来查找android的多项式的根。目前我只是在处理二次方检验,但我无法发现我的错误。无论我做了什么,这意味着我对根的价值(现在只做第一根),总是0.0。早些时候我试图找到我的判断力的价值,我不断得到“b”这个词的平方'在二次方。由于应用程序的设置方式,我的二次方程看起来很奇怪,所以字母移位1,所以我的app中的quadratics的闭合形式解是 - (c(+/-)(c ^ c-4 * b * d )^(1/2))/(2 * b)中。我将不胜感激,谢谢! :) 附:请尽量忽略我糟糕的代码,我现在只是想让它工作:)

public void onButtonClick(View v)
{
double rx, ry, rz, disc, disc2, a, b, c, d;
String sa, sb, sc, sd, sx, sy, sz;
EditText ta = (EditText)findViewById(R.id.a_c);
EditText tb = (EditText)findViewById(R.id.b_c);
EditText tc = (EditText)findViewById(R.id.c_c);
EditText td = (EditText)findViewById(R.id.d_c);
EditText tx = (EditText)findViewById(R.id.x);
EditText ty = (EditText)findViewById(R.id.y);
EditText tz = (EditText)findViewById(R.id.z);
sa = ta.getText().toString();
sb = tb.getText().toString();
sc = tc.getText().toString();
sd = td.getText().toString();
sx = tx.getText().toString();
sy = ty.getText().toString();
sz = tz.getText().toString();
if(sa.matches("")) {a = 0;} else {a = Double.parseDouble(sa);}
if(sb.matches("")) {b = 0;} else {b = Double.parseDouble(sb);}
if(sc.matches("")) {c = 0;} else {c = Double.parseDouble(sc);}
if(sd.matches("")) {d = 0;} else {d = Double.parseDouble(sa);}
if(sx.matches("")) {rx = 0;} else {rx = Double.parseDouble(sx);}
if(sy.matches("")) {ry = 0;} else {ry = Double.parseDouble(sy);}
if(sz.matches("")) {rz = 0;} else {rz = Double.parseDouble(sz);}
disc2 = c*c-4*b*d;
if(a == 0) {rx = (-c+Math.sqrt(disc2))/(2*b); tx.setText(Double.toString(rx));}
}

1 个答案:

答案 0 :(得分:0)

以下是我如何求解二次方程。首先对此进行排序,然后担心进出数据:

/**
 * Quadratic root finder
 * @link http://stackoverflow.com/questions/23956437/quadratic-formula-in-java-for-android-cant-spot-my-mistake
 */
public class QuadraticRootFinder {

    private static final double TOLERANCE = 1.0e-8;

    public static void main(String[] args) {
        if (args.length > 0) {
            double a = Double.parseDouble(args[0]);
            double b = ((args.length > 1) ? Double.parseDouble(args[1]) : 0.0);
            double c = ((args.length > 2) ? Double.parseDouble(args[2]) : 0.0);
            Complex [] roots = QuadraticRootFinder.getRoots(a, b, c);
            for (Complex root : roots) {
                System.out.println(root);
            }
        } else {
            System.out.println("Usage: QuadraticRootFinder <a> <b> <c>");
        }
    }


    public static Complex [] getRoots(double a, double b, double c) {
        Complex [] roots = new Complex[2];
        if (Math.abs(a) <= TOLERANCE) {  // Linear equation; just one root
            if (Math.abs(b) > TOLERANCE) {
                roots = new Complex[1];
                roots[0] = new Complex(-c/b);
            } else {
                throw new IllegalArgumentException(String.format("No roots possible for (a,b,c) = (%f10.3, %f10.3, %f10.3", a, b, c));
            }
        } else {
            double discriminant = b*b-4.0*a*c;
            if (discriminant > 0.0) {  // Two real roots
                roots[0] = new Complex(-b/2.0/a-Math.sqrt(discriminant)/2.0/a, 0.0);
                roots[1] = new Complex(-b/2.0/a+Math.sqrt(discriminant)/2.0/a, 0.0);
            } else { // Two complex conjugate roots
                roots[0] = new Complex(-b/2.0/a, -Math.sqrt(-discriminant)/2.0/a);
                roots[1] = new Complex(-b/2.0/a, +Math.sqrt(-discriminant)/2.0/a);
            }
        }
        return roots;
    }

}

class Complex {
    private final double x;
    private final double y;

    Complex() {
        this(0.0, 0.0);
    }

    Complex(double x) {
        this(x, 0.0);
    }

    Complex(double x, double y) {
        this.x = x;
        this.y = y;
    }

    double getX() {
        return x;
    }

    double getY() {
        return y;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("(").append(x).append(",").append(y).append(")");
        return builder.toString();
    }
}