关于一道类的问题
import java.util.Scanner;public class TestQuadraticEuqation {
public static void main(String[] args){
//Scanner input = new Scanner(System.in);
//System.out.print("please input a,b,c:");
//double x = input.nextDouble();
//double y = input.nextDouble();
//double z = input.nextDouble();
QuadraticEuqation quadraticEuqation = new QuadraticEuqation(0,0,0);
quadraticEuqation.geta();
quadraticEuqation.getb();
quadraticEuqation.getc();
if(quadraticEuqation.getDiscriminant()>0)
System.out.println("The equation has two roots,root1 = : "+quadraticEuqation.getRoot1()+"root2 = "+quadraticEuqation.getRoot2());
else if(quadraticEuqation.getDiscriminant()==0)
System.out.println("The equation has the same root,root = "+quadraticEuqation.getRoot1());
else
System.out.println("The equaton has no roots");
}
}
class QuadraticEuqation{
Scanner input = new Scanner(System.in);
private double a;
private double b;
private double c;
QuadraticEuqation(double newa,double newb,double newc){
a = newa;
b = newb;
c = newc;
}
public double geta(){
System.out.print("please input a:");
double a = input.nextDouble();
return a;
}
public double getb(){
System.out.print("please input b:");
double b = input.nextDouble();
return b;
}
public double getc(){
System.out.print("please input c:");
double c = input.nextDouble();
return c;
}
public double getDiscriminant(){
return b*b-4*a*c;
}
public double getRoot1(){
return (-1*b+Math.pow(getDiscriminant(),1/2))/(2*a);
}
public double getRoot2(){
return (-1*b-Math.pow(getDiscriminant(),1/2))/(2*a);
}
}
错误是
please input a:1
please input b:2
please input c:1
The equation has the same root,root = Infinity
请大神指教