请帮助我用函数方法解决二次方程。如何通过函数方法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;
}
}
我没有看到你在哪里定义'tenlikkok'或'tenlikD'方法。 – azurefrog
相似? http://stackoverflow.com/questions/26813045/solving-quadratic-equation-using-methods-java?rq=1 – LeHill
你的代码似乎有一些问题。我相信你的问题的答案是'System.out.println(Function.root(a,b));'。但是,'root()'返回判别式,所以这就是它将打印的内容,除了'root()'打印自己的内容。 –