Java 为什么赢了';这个二次方程不返回负数吗?

Java 为什么赢了';这个二次方程不返回负数吗?,java,math,quadratic,Java,Math,Quadratic,这个二次方程不会返回我确定返回的字符串中的负数。 下面是方程式: public class QuadraticEquation { String final0; public String calculate(int a, int b, int c) { double done1 = ((-1 * b) + Math.sqrt((b * b) - (4 * a * c))) / (2 * a); double done2 = ((-1 * b)

这个二次方程不会返回我确定返回的字符串中的负数。 下面是方程式:

public class QuadraticEquation  {

    String final0;
    public String calculate(int a, int b, int c) {
        double done1 = ((-1 * b) + Math.sqrt((b * b) - (4 * a * c))) / (2 * a);
        double done2 = ((-1 * b) - Math.sqrt((b * b) - (4 * a * c))) / (2 * a);
        final0 = "x = " + (done1) + " or x = " + (done2);

        return final0;
    }
}
想象一个a、b和c值为-3、13和-4的方程。该值的返回值将为-0.3(重复)和-4。但是这个等式只返回正的值,所以在这种情况下,它将返回0.3(重复)和4。为什么会这样,我能做些什么来修复它

注意:我确实认为这是一个Java错误,而不是一个数学错误。如果这是一个数学错误,请在评论中告诉我,我会及时把它放在适当的论坛上。谢谢

我会想

-3*x^2 + 13 *x + -4 = -3 * (x - 0.33333) * (x - 4) = 0
所以有两个肯定的答案是正确的

试试看

1 * x^2 + 0 * x -1 = (x - 1) * (x + 1) = 0
i、 e.x=-1或+1


下面是我将如何写它

public static String calculate(int a, int b, int c) {
    double sqrt = Math.sqrt((b * b) - (4 * a * c));
    double done1 = (-b + sqrt) / (2 * a);
    double done2 = (-b - sqrt) / (2 * a);
    return "x = " + (done1) + " or x = " + (done2);
}
我会想

-3*x^2 + 13 *x + -4 = -3 * (x - 0.33333) * (x - 4) = 0
所以有两个肯定的答案是正确的

试试看

1 * x^2 + 0 * x -1 = (x - 1) * (x + 1) = 0
i、 e.x=-1或+1


下面是我将如何写它

public static String calculate(int a, int b, int c) {
    double sqrt = Math.sqrt((b * b) - (4 * a * c));
    double done1 = (-b + sqrt) / (2 * a);
    double done2 = (-b - sqrt) / (2 * a);
    return "x = " + (done1) + " or x = " + (done2);
}

代码与您的输入正常工作。例如,如果您将b更改为-13,您将得到

x=-4.0或x=-0.3333


Math.sqrt将始终返回一个正数,忽略复数。但这有点离题。

代码与您的输入正确配合。例如,如果您将b更改为-13,您将得到

public static void main(String[] args) {

    String final0 = calculate(-3, 13, -4);
    System.out.println(final0);

}
public static String calculate(int a, int b, int c) {
    String final0 ;
    int i = -1 * b; // -1 * 13 = -13
    System.out.println(i);

    int j = 4 * a * c; // 4 * -3 * -4  = 4 * 12  = 48
    System.out.println(j);

    double sqrt = Math.sqrt((b * b) - j); // sqrt ((13 * 13) - 48) = sqrt(169 - 48) = sqrt(121) = 11
    System.out.println(sqrt);

    double d = i + sqrt; // -13 + 11 = -2
    System.out.println(d);

    int k = 2 * a; // 2* -3 = -6
    System.out.println(k);

    double done1 = d / k; // -2 / -6 = 1/3 = 0.3333333333
    System.out.println(done1);

    double done2 = (i - sqrt) / k;
    final0 = "x = " + (done1) + " or x = " + (done2);

    return final0;
}
x=-4.0或x=-0.3333

Math.sqrt将始终返回一个正数,忽略复数。但这有点离题

public static void main(String[] args) {

    String final0 = calculate(-3, 13, -4);
    System.out.println(final0);

}
public static String calculate(int a, int b, int c) {
    String final0 ;
    int i = -1 * b; // -1 * 13 = -13
    System.out.println(i);

    int j = 4 * a * c; // 4 * -3 * -4  = 4 * 12  = 48
    System.out.println(j);

    double sqrt = Math.sqrt((b * b) - j); // sqrt ((13 * 13) - 48) = sqrt(169 - 48) = sqrt(121) = 11
    System.out.println(sqrt);

    double d = i + sqrt; // -13 + 11 = -2
    System.out.println(d);

    int k = 2 * a; // 2* -3 = -6
    System.out.println(k);

    double done1 = d / k; // -2 / -6 = 1/3 = 0.3333333333
    System.out.println(done1);

    double done2 = (i - sqrt) / k;
    final0 = "x = " + (done1) + " or x = " + (done2);

    return final0;
}
如果您将方法分解为更多的局部变量,您将看到java中的数学正确工作


如果您将方法分解为更多的局部变量,您将看到java中的数学运算是正确的。

@assylias sqrt返回double.@ZiyaoWei,但您随后除以
(2*a)
,这是一个整数。这也不会处理虚数。a、 b和c应该是双倍的。正确的答案不是只有0.3吗?(-1*13+sqrt(13*13-4*-3*-4))/(2*-3)=-13+11/-6)=0.333我知道它不会处理虚数。我不想处理这个问题。我还是不明白。@ZiyaoWei是的。您应该将其作为答案发布。@assylias sqrt返回double。@ZiyaoWei,但您需要除以
(2*a)
,这是一个整数。这也不会处理虚数。a、 b和c应该是双倍的。正确的答案不是只有0.3吗?(-1*13+sqrt(13*13-4*-3*-4))/(2*-3)=-13+11/-6)=0.333我知道它不会处理虚数。我不想处理这个问题。我还是不明白。@ZiyaoWei是的。你应该把它作为答案贴上去。@ZiyaoWei正确。谢谢。@ZiyaoWei正确。非常感谢。