Java基础编程题目——求方程 ax^2+bx+c=0 的根

求方程求 ax^2 + bx + c = 0 的根

import  java.util.Scanner;

public class Gen {
    public static void main(String[] args) {
        System.out.println("求 ax^2 + bx + c = 0 的根");
        Scanner input = new Scanner(System.in);
        System.out.println("请输入a值:");
        double a = input.nextDouble();
        System.out.println("请输入b值:");
        double b = input.nextDouble();
        System.out.println("请输入c值:");
        double c = input.nextDouble();
        double x1 = ((-b + Math.sqrt(b*b - 4*a*c)) / (2*a));
        double x2 = ((-b - Math.sqrt(b*b - 4*a*c)) / (2*a));
        if (b*b - 4*a*c >= 0) {
            System.out.println("x1 = " + x1);
            System.out.println("x2 = " + x2);
        }
        else
            System.out.println("此方程无实根");
    }
}

你可能感兴趣的:(Java编程题目)