如何调用带参数的方法?

时间:2016-03-19 00:08:33

标签: java methods parameters call

好的,我想制作一个程序,既可以找到二次方程的判别式,也可以找到根的数量。到目前为止,我创建了程序以找到判别式,但是我在调​​用方法来查找根数时遇到了麻烦。有人可以向我解释一下这是怎么做的吗?感谢。

 public class quadMethods
 {
     public static void main (String args[])
     {
         new quadMethods ();
     }


     public quadMethods ()
     {
         System.out.println ("The discriminant is: " + discrim (1, 6, 8));
         System.out.println ("The number of roots is: " + numRoots (1, 6, 8));
     }
  public double discrim (double a, double b, double c)
     { //assumes ax^2+bx+c=0
         //returns the discriminant of the quadratic equation
         //b*b-4*a*c
         //replace the return 1
    double discriminant = b*b-4*a*c;    
         return discriminant;
     }


     public int numRoots (double a, double b, double c)
     { //assumes ax^2+bx+c=0
         //returns the number of roots for the quadratic equation
         //call discrim method, make an if to return 0, 1, or 2.
         //replace the return 1
  return 1;
     }
 }

3 个答案:

答案 0 :(得分:0)

public static void main (String args[])
{
    quadMethods instance = new quadMethods();

    System.out.println(instance.discrim(1.0, 2.0, 3.0));

    int myRoots = instance.numRoots(1.0, 2.0, 3.0);
}

答案 1 :(得分:0)

您需要创建一个对象并调用方法

<table class="second">

到目前为止我在做什么我只是创建一个对象(但是匿名)......

答案 2 :(得分:0)

哦顺便说一下,我正在寻找的答案是:

{{1}}