猿问

二次方程 Java 类

我正在用 Java 为学校工作,到目前为止,我在处理课程和他们的作业问题时遇到了一些麻烦。我对二次方程类有以下标准:


到目前为止,我有:


private static double coefA;

private static double coefB;

private static double coefC;



public static void main(String[] args) 

{

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the a, b and c for a Quadratic: ");

    coefA = input.nextDouble();

    coefB = input.nextDouble();

    coefC = input.nextDouble();


    double discriminant = getDiscriminant();


    if (discriminant < 0)

    {

        System.out.println("There are no real roots.");


    }

    else if (discriminant == 0)

    {

        System.out.println("The one root is: "+getRoot1());

    }

    else

    {

        System.out.println("The first root is: "+getRoot1());

        System.out.println("The second root is: "+getRoot2());

    }


}

//Construct

public QuadraticEquation(double a, double b, double c)

{

    coefA = a;

    coefB = b;

    coefC = c;

}


private static double getDiscriminant()

{

    double discriminant = (coefB * coefB) - (4 * coefA * coefC);

    return discriminant;

}

static double getRoot1()

{

    double root1 = (-coefB + Math.sqrt(getDiscriminant()))/ 2 * coefA;

    return root1;


}

static double getRoot2()

{

    double root2 = (-coefB - Math.sqrt(getDiscriminant()))/ 2 * coefA;

    return root2;


}

}

这个等式不起作用,我什至认为我不符合标准,但我不完全理解这本书的要求。任何人都可以提供帮助吗?


白猪掌柜的
浏览 228回答 2
2回答

慕婉清6462132

你的数学方程的实现是正确的,但你必须放在括号里。例如 ;double root1 = (-coefB + Math.sqrt(getDiscriminant()))/ 2 * coefA;这里(-coefB + Math.sqrt(getDiscriminant()))方程被除以通过2。在此之后乘用coefA。小心点。用这个例子改变你的逻辑;double root1 = (-coefB + Math.sqrt(getDiscriminant()))/ (2 * coefA);所以适用于其他领域以获得正确的结果。你的两种方法应该改变。static double getRoot1(){&nbsp; &nbsp; double root1 = (-coefB + Math.sqrt(getDiscriminant()))/ (2 * coefA);&nbsp; &nbsp; return root1;}static double getRoot2(){&nbsp; &nbsp; double root2 = (-coefB - Math.sqrt(getDiscriminant()))/ (2 * coefA);&nbsp; &nbsp; return root2;}更详细的,运算符优先级表;Operators&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Precedence&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Associativitypostfix increment and decrement&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;++ --&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left to rightprefix increment and decrement, and unary&nbsp; &nbsp; &nbsp; &nbsp;++ -- + - ~ !&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right to leftmultiplicative&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * / %&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left to rightadditive&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left to right

UYOU

对于 QuadraticEquation 类来说,这不是一个好的设计。三个变量(coefA、coefB、coefC)应该是实例变量,而不是静态的。您应该有一个将三个值作为输入的构造函数。构造函数调用 getDiscriminant() 并计算两个可能的答案,并使用 getter 检索它们的效率要高得多。
随时随地看视频慕课网APP

相关分类

Java
我要回答