0

基本上,我正在尝试编写一个程序,为您提供具有三个变量的二次方程的判别式。但是,当我尝试创建一个具有二次方的 ab 和 c 值的对象时,它说我没有创建该对象。另外我是新人,所以如果我做了明显错误的事情,请原谅我。

这是我得到的错误。

线程“主”java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的树类型:在 quadratic.equation.solver.QuadraticEquationSolver.main(QuadraticEquationSolver.java:38) Java 结果:1

下面是代码。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quadratic.equation.solver;

/**
 *
 * @author User
 */
public class QuadraticEquationSolver {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    class Quadratic {

        int aValue;
        int bValue;
        int cValue;

        public Quadratic(int A, int B, int C) {
            aValue = A;
            bValue = B;
            cValue = C;
        }

        public int calculateDiscriminant(int A, int B, int C) {
            int answer = ((bValue*bValue)+(-4*aValue*cValue));
            return answer;
        }

        Quadratic firstQuad = new Quadratic(7, 5, 3); 

     } 
     System.out.println(firstQuad.calculateDiscriminant);
}
4

1 回答 1

1

这是更清晰的解决方案。

public class Quadratic {


    private int aValue;
    private int bValue;
    private int cValue;

   //constructor
   public Quadratic(int a, int b, int c) {
      aValue = a;
      bValue = b;
      cValue = c;
    }

  public int calculateDiscriminant() {
    int answer = ((bValue*bValue)+(-4*aValue*cValue));
    return answer;
  }

}//end class

现在是一个测试班。

public class Test{    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Quadratic firstQuad = new Quadratic(7, 5, 3); 
        System.out.println(firstQuad.calculateDiscriminant());

    } 

}

要不就

public final class MathUtil {

private MathUtil(){}

 public static int calculateQuadraticDiscriminant(int aValue,int bValue, int cValue) {
        return ((bValue*bValue)+(-4*aValue*cValue));        
 }

}
于 2013-06-22T20:55:23.023 回答