二次方程求解器无法在Java中得到结果

1 java quadratic

我有代码应该解决二次方程,但结果产生NaN.

我现在环顾了两天,我找不到解决方案.任何和所有的建议将不仅仅是赞赏!

package quadratic;

import java.util.Scanner;

public class Formlua {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("enter value of A ");
        double a = input.nextDouble();
        System.out.println("enter value of B ");
        double b = input.nextDouble();
        System.out.println("enter value of C ");    
        double c = input.nextDouble();
        double four = 4;
        double square = Math.sqrt(b* b - 4 * a * c );

        double root1 = (-b + square) / (2*a);

        double root2 = (-b - square) / (2*a);   
        System.out.println("The answer is " + root1 + "and" + root2);

        System.out.println("Do you want to continue? y/n");

        String user = input.toString();
        if(user.equalsIgnoreCase("y"));
    }
}
Run Code Online (Sandbox Code Playgroud)

mas*_*nik 6

这段代码:

Math.sqrt(b* b - 4 * a * c );
Run Code Online (Sandbox Code Playgroud)

可以导致NaN("不是数字").

如果值为b* b - 4 * a * c负,则只有复数的解(但不是双数据类型)

应该有一个条件

if (b* b - 4 * a * c<0) {
    System.out.println("There is no solution in real numbers");
    return;
}
Run Code Online (Sandbox Code Playgroud)