算方程式的根

import java.util.Scanner;
public class Equation {
    public static void main(String[] args){
        double r1,r2;
        Scanner input = new Scanner(System.in);
        System.out.println("请输入a,b,c的值:");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        r1 = (-b + Math.pow((b * b - 4 * a *c), 0.5)) / (2 * a);
        r2 = (-b - Math.pow((b * b - 4 * a *c), 0.5)) / (2 * a);
        if (b * b - 4 * a * c < 0){
            System.out.println("The equation has no real roots");
        }
        else if (b * b - 4 * a * c == 0){
            System.out.println("The root is " + r1);
        }
        else{
            System.out.println("The root is " + r1 + r2);
        }    
    }
}

计算ax2+bx+c=0的根

posted @ 2014-08-07 15:12  Amoxicil  阅读(157)  评论(0编辑  收藏  举报