我需要一些帮助,我正在尝试创建一个程序。这是一个二次方程公式,其中我有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
我们不知道该找什么。没有人会接受这些代码,在自己的计算机上编译它,制作样本测试用例,将结果与结果进行比较,然后让他们知道是否发现任何错误。但是,如果你让他们知道发生了什么问题,他们会发现任何错误。告诉我们发生了什么,以及发生了什么。你有任何错误?等等... –