0

我正在尝试创建一个解决二次方程(a x 2 + b x + c = 0)的程序。当我删除它时它工作正常,Math.sqrt(delta)但拥有它是该过程的一个非常重要的部分,我无法弄清楚它为什么不起作用。

当我运行程序时,它最后给了我X1 = Nan X2 = Nan

public static void main(String[] args) {
    double a, b, c, x1, delta;
    
    Scanner sc = new Scanner(System.in);
    System.out.println("Entrez Les Valeurs De a, b et c: ");
    a = sc.nextInt();
    b = sc.nextInt();
    c = sc.nextInt();
    
    delta = (b * b) - 4 * ( a * c );


    x1 = (-b + Math.sqrt( delta ) )/(2 * a);
    
    
    System.out.println("X1= " + x1);
                    
    
}
4

1 回答 1

3

如果 delta 结果是负数,sqrt将返回NaN

于 2020-12-14T18:44:24.770 回答