2016-09-19 92 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) +"."); 
      } 

     } 
    }