Java半分支错误的二次公式

时间:2016-09-26 19:39:24

标签: java syntax-error formula quadratic

我编写了用于在Java中查找二次公式的根的代码。但是,我得到的错误是我在第6行错过了分号,即使我有一个分号。发生了什么事?

public class Lab2{
   public static void main (String[] args){
    double a = 1;
    double b = 2;
    double c = 4;
    double d = (math.pow(b, 2))-(4*a*c));

    if (d < 0)
    {
        String.out.println("There are no real roots.");
     } else if (a == 0) {
        x3 = (-1)*c*b;
        String.out.println("The root is " + x3);
     } else if (a != 0) {
        x1 = (((-1)*b) + (d))/(2*a);
        x2 = (((-1)*b) - (d))/(2*a);
        String.out.println(" The roots are " + x1 + " and " + x2 + ".");
     }



  }
} 

1 个答案:

答案 0 :(得分:1)

    double d = (math.pow(b, 2))-(4*a*c));

这一行有一个额外的右括号。

试试这个:

    double d = (Math.pow(b, 2))-(4*a*c);