2016-09-19 109 views
1

语法错误“;”,,预计在行“双a,b,c,判别式,根;”错误“;”,,变量声明后预期

如何解决此错误?

public class Quadratic { 

    double a, b, c, discriminant, root; 

    discriminant = (b * b) - 4 * a * c; 

    public Quadratic(double a, double b, double c) { 
    } 

    public String calculateroots() { 

     if (discriminant >= 0){ 
      root = Math.sqrt(discriminant)/(2 * a); 

      System.out.println("Your roots are " + (-1 * b) + "+" + root + "and" + (-1 * b) + (-1 * root) +"."); 
     } 
     else { 
      root = Math.sqrt(Math.abs(discriminant))/(2 * a); 

      System.out.println("Your roots are " + (-1 * b) + "+ i" + root + "and" + (-1 * b) + "i" + (-1 * root) +"."); 
     } 
     } 
    } 
+3

该代码有几个问题。从以下开始:判别式=(b * b)-4 * a * c;需要在方法或初始化块中 – Stultuske

+0

提示:请阅读Java命名约定。方法名称go camelCase!然后:阅读关于java语法。你不能把陈述放在你认为他们可能适合的地方...... – GhostCat

+0

我建议在构造函数中设置你的实例变量,然后在那里求解判别式。 '公共二次(double a,double b,double c){this.a = a; this.b = b; this.c = c;判别式=(b * b)-4 * a * c; }' – Orin

回答

-1

你在这里。

public class Quadratic { 

    double a, b, c, discriminant, root; 

    public Quadratic(double a, double b, double c) { 
     discriminant = (b * b) - 4 * a * c; 
    } 

    public void calculateroots() { 

     if (discriminant >= 0) { 
      root = Math.sqrt(discriminant)/(2 * a); 

      System.out.println("Your roots are " + (-1 * b) + "+" + root + "and" + (-1 * b) + (-1 * root) + "."); 
     } else { 
      root = Math.sqrt(Math.abs(discriminant))/(2 * a); 

      System.out.println("Your roots are " + (-1 * b) + "+ i" + root + "and" + (-1 * b) + "i" + (-1 * root) + "."); 
     } 
    } 

} 
+1

第一:只有代码答案是不好的。第二:你不觉得初始化'a','b'和'c'也是有意义的吗? – Tom

-1

只是回答你的问题......语法错误是由

discriminant = (b * b) - 4 * a * c;

判别的位置分配(a, b ,c)未初始化的变量引起的。

我建议把它变成calculateroots()并更改返回类型为void:

public class Quadratic { 

    double a, b, c, discriminant, root; 

    public Quadratic(double a, double b, double c) { 
     this.a = a; 
     this.b = b; 
     this.c = c; 
    } 

    public void calculateroots() { 

     discriminant = (b * b) - 4 * a * c; 

     if (discriminant >= 0){ 
      root = Math.sqrt(discriminant)/(2 * a); 

      System.out.println("Your roots are " + (-1 * b) + "+" + root + "and" + (-1 * b) + (-1 * root) +"."); 
     } 
     else { 
      root = Math.sqrt(Math.abs(discriminant))/(2 * a); 

      System.out.println("Your roots are " + (-1 * b) + "+ i" + root + "and" + (-1 * b) + "i" + (-1 * root) +"."); 
     } 
     } 
    } 

与您共创这种方式只是实例二次,并给予价值的构造函数abc然后调用calculateroots()

+0

不!它不是“**判别式被分配(a,b,c)未初始化的变量。”“ – eRaisedToX

+0

不!我没有,因为如果你写了类似双myvar = a;这不是一个错误。你所说的尝试与问题是一样的......即你试图重新定义一个变量......这是不允许的! – eRaisedToX

+0

谢谢你的朋友指点!尽管这是一个简短的形式..'bcoz'!!! 但编辑为像你这样的人谁发现很难理解这样的简单单词 – eRaisedToX

0

小心构造函数中的变量初始化为discriminant初始化为calculateroots

public class Quadratic { 

     double a, b, c, discriminant, root; 

     public Quadratic(double a, double b, double c) { 
      this.a = a; 
      this.b = b; 
      this.c = c; 
     } 

     public void calculateroots() { 
      discriminant = (b * b) - 4 * a * c; 

      if (discriminant >= 0){ 
       root = Math.sqrt(discriminant)/(2 * a); 

       System.out.println("Your roots are " + (-1 * b) + "+" + root + "and" + (-1 * b) + (-1 * root) +"."); 
      } 
      else { 
       root = Math.sqrt(Math.abs(discriminant))/(2 * a); 

       System.out.println("Your roots are " + (-1 * b) + "+ i" + root + "and" + (-1 * b) + "i" + (-1 * root) +"."); 
      } 

     } 
    }