以下是引用付政委在2011-4-5 23:00:12的发言:
import java.util.*;
class JieHanShu {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a, b, c, x1, x2, d;
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();
if (a == 0)
{
if (b == 0)
{
if (c == 0)
System.out.println("方程的根是任意实数");
else
System.out.println("方程无根");
}
else
{
System.out.println("x=" + (-b / c));
}
}
else
{
d = b * b - 4 * a * c;
if (d > 0) {
System.out.println("方程有两个实根");
x1 = (-b + Math.sqrt(d)) / 2 / a;
x2 = (-b - Math.sqrt(d)) / 2 / a;
System.out.println("x1=" + x1);
System.out.println("x2=" + x2);
} else if (d == 0) {
System.out.println("方程有一个实根");
x1 = -b / 2 / a;
System.out.println("x1=" + x1);
} else {
System.out.println("方程有两个虚根");
System.out.println("x1=" + (-b / 2 / a) + "+" + Math.sqrt(-d)
/ 2 / a + "i");
System.out.println("x1=" + (-b / 2 / a) + "-" + Math.sqrt(-d)
/ 2 / a + "i");
}
}
}
}
这个例子不是很好,是完全面向过程思想做的,如果用面向对象思想会更好些