设计一个一元二次方程类,并为这个类添加异常处理
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.lang.Math;
class Equation
{
public static void main(String[] args)
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(read);
int a=0,b=0,c=0;
double x1,x2;
System.out.println("请依次输入a,b,c的值,我们将会为你计算方程ax*x+bx+c=0的根(如果存在的话)");
try
{
for(int i = 0;i < 3;i++)
{
if(i == 0)
a = Integer.parseInt(buf.readLine());
else
{
if(i == 1)
b = Integer.parseInt(buf.readLine());
else
c = Integer.parseInt(buf.readLine());
}
}
}
catch(Exception e)
{
System.out.println(e);
}
if (b*b-4*a*c<0)
{
System.out.println("方程"+a+"x^2+"+b+"x"+c+"=0无解");
}
else
{
x1 = (-b+Math.sqrt(b*b-4*a*c))/2*a;
x2 = (-b-Math.sqrt(b*b-4*a*c))/2*a;
System.out.println("方程"+a+"x^2+"+b+"x+"+c+"=0的根:");
System.out.println("x1="+x1+","+"x2="+x2);
}
}
}