2016-09-06 82 views
1

请帮助我用函数方法解决二次方程。如何通过函数方法java解决二次方程?

我有2类:

void类和function类。

我想打电话给function类来解决一个二次方程。我怎样称呼function课程? void类:

import java.util.Scanner; 

public class task53 { 

    public static void main(String[] args) { 

    Scanner sc= new Scanner (System.in); 
    double a, b,c; 
    System.out.println("please input int for Quadratic equation"); 

    System.out.println(" a -int input "); 
    a= sc.nextInt(); 
    System.out.println(" b -int input "); 
    b= sc.nextInt(); 
    System.out.println(" c -int input "); 
    c= sc.nextInt(); 

    System.out.println(Function.tenlikkok(a, b)); 

    } 
} 

function类:

public class Function { 
    public static double D(double a, double b, double c, double resultD) { 

    resultD = Math.pow(b,2) - 4*a*c; 
    return resultD(); 

    } 

    public static double root (double a, double b) { 

    double D =Function.tenlikD(a, b, c, resultD); 


    if (D > 0){ 
     double x1,x2; 
     x1 = (-b - Math.sqrt(D))/(2*a); 
     x2 = (-b + Math.sqrt(D))/(2*a); 
     System.out.println(" x1 = " +x1 + " x2 = " +x2); 
    } 
    else if (D==0){ 
     double x; 
     x = -b/(2 * a); 
     System.out.println(" x1 = x2 = " + x); 
     } else { 
     System.out.println("no root"); 
    } 

    return D; 
    } 
} 
+0

我没有看到你在哪里定义'tenlikkok'或'tenlikD'方法。 – azurefrog

+1

相似? http://stackoverflow.com/questions/26813045/solving-quadratic-equation-using-methods-java?rq=1 – LeHill

+0

你的代码似乎有一些问题。我相信你的问题的答案是'System.out.println(Function.root(a,b));'。但是,'root()'返回判别式,所以这就是它将打印的内容,除了'root()'打印自己的内容。 –

回答

0

虽然代码是相当不清楚,因为是你的问题,我相信你试图实现从类Function方法task53类,也许?要做到这一点,你可以在此线下public class task53{

Function func = new Function();

,并通过做func.method()Function实现的方法。

+0

因为'Function'只包含静态方法,所以在实例化它时没有意义(在'new Function()'中没有任何一点)。只是为了执行'Function.staticMethod()'(或者使这些方法是非静态的)而被认为是更好的风格。 –