語法錯誤「;」,,預計在行「雙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) +".");
}
}
}
該代碼有幾個問題。從以下開始:判別式=(b * b)-4 * a * c;需要在方法或初始化塊中 – Stultuske
提示:請閱讀Java命名約定。方法名稱go camelCase!然後:閱讀關於java語法。你不能把陳述放在你認爲他們可能適合的地方...... – GhostCat
我建議在構造函數中設置你的實例變量,然後在那裏求解判別式。 '公共二次(double a,double b,double c){this.a = a; this.b = b; this.c = c;判別式=(b * b)-4 * a * c; }' – Orin