我需要一些幫助,我正在嘗試創建一個程序。這是一個二次方程公式,其中我有2個類。二次公式解決方案問題
是我收到的唯一問題是代碼
"QuadraticEquation Equation = new QuadraticEquation(a, b, c);"
我收到寫着錯誤:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type TestQuadraticEquation is accessible. Must qualify the allocation with an enclosing instance of type TestQuadraticEquation (e.g. x.new A() where x is an instance of TestQuadraticEquation).
at TestQuadraticEquation.main(TestQuadraticEquation.java:12)
此錯誤是發生在第12行(上面的代碼),我需要找出該部分有什麼問題。
public class TestQuadraticEquation
{
public static void main (String [] args)
{
java.util.Scanner scan = new java.util.Scanner(System.in);
System.out.println("Please enter the coefficients a, b and c in the order: ");
double a = scan.nextDouble();
double b = scan.nextDouble();
double c = scan.nextDouble();
QuadraticEquation Equation = new QuadraticEquation(a, b, c);
if (Equation.getDiscriminant() > 0)
{
System.out.println("The roots of the equations are " + Equation.getRoot1()
+ " and " + Equation.getRoot2());
}
else
{
System.out.println("The equation has no roots.");
}
}
class QuadraticEquation
{
private double a;
private double b;
private double c;
QuadraticEquation()
{
a = 0;
b = 0;
c = 0;
}
QuadraticEquation (double newA, double newB, double newC)
{
a = newA;
b = newB;
c = newC;
}
public double getA()
{
return a;
}
public double getB()
{
return b;
}
public double getC()
{
return c;
}
public double getDiscriminant()
{
return (Math.pow(b,2) - 4 * a * c);
}
public double getRoot1()
{
return ((-b + Math.sqrt(getDiscriminant())/(2 * a)));
}
public double getRoot2()
{
return ((-b - Math.sqrt(getDiscriminant())/(2 * a)));
}
}
}
只是一個友情提示,你可以想要閱讀本頁面:[如何做問題指南](https://stackoverflow.com/help/how-to-ask),以便您始終可以確定您的問題很容易回答並且儘可能清晰。一定要包括你爲解決你遇到的問題所做的任何努力,以及當你嘗試修復這些問題時發生了什麼。另外不要忘記你的顯示代碼和任何錯誤信息! –
你是什麼意思「什麼是問題」?我沒有看到任何問題。 – ajb
我們不知道該找什麼。沒有人會接受這些代碼,在自己的計算機上編譯它,製作樣本測試用例,將結果與結果進行比較,然後讓他們知道是否發現任何錯誤。但是,如果你讓他們知道發生了什麼問題,他們會發現任何錯誤。告訴我們發生了什麼,以及發生了什麼。你有任何錯誤?等等... –