2012-12-03 52 views
-2
void quadratic() { 

    if((b*b-4*a*c) < 0){ 
     System.out.println("The answer is imaginary."); 
    } 
    else { 
     System.out.println("The two root's x values of the quadratic function " + a +"x^2 + " + b + "x + " + c + " are " 
        + (-b + Math.sqrt(b*b+4*a*c)/2a)); 
    } 
} 

我得到的错误为什么我在这里遇到这个错误?

')' 预期 ';'预计

上的最后一行代码,即使它们已被包括在内。我如何防止这种情况?任何帮助将非常感激。

+0

简化代码,直到问题消失。然后添加代码直到问题重新出现。这将是明显的问题是什么。 –

+0

更改2a至2 * a – hoaz

+1

'2a'无效;要相乘,必须包含运算符:'2 * a'。 – Vulcan

回答

5

您2之间缺少操作员和

Math.sqrt(b*b+4*a*c)/2a 

应该是这样的:

Math.sqrt(b*b+4*a*c)/(2*a) 
1

变化2a(2*a)

Math.sqrt(b*b+4*a*c)/(2*a) 

您还可以分解出计算的判例,以避免必须做两次:

double discriminant = (b*b-4*a*c); 
    if(discriminant < 0){ 
     System.out.println("The answer is imaginary."); 
    } 
    else { 
     System.out.println("The two root's x values of the quadratic function " + a +"x^2 + " + b + "x + " + c + " are " 
       + (-b + Math.sqrt(discriminant)/(2*a))); 
    } 
1

你不能写

/2a 

这是混淆解析器。我期望你想要的是

/(2 * a)