分支-18. 求一元二次方程的根

import java.util.Scanner;

public class Main {

	/**
	 * 分支-18. 求一元二次方程的根
	 */
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		double a = s.nextDouble();
		double b = s.nextDouble();
		double c = s.nextDouble();

		if (a == 0 && b == 0 && c != 0) {
			System.out.println("Not An Equation");
			return;
		}

		if (a == 0 && b == 0 && c == 0) {
			System.out.println("Zero Equation");
			return;
		}

		if (a == 0) {
			double x = -c / b;
			System.out.printf("%.2f", x);
			return;
		}

		double d = b * b - 4 * a * c;
		if (d > 0) {
			double x1 = (-b + Math.sqrt(d)) / (2 * a);
			double x2 = (-b - Math.sqrt(d)) / (2 * a);

			System.out.printf("%.2f", x1);
			System.out.println();
			System.out.printf("%.2f", x2);
		} else if (d < 0) {
			double a1 = -b / (2 * a);
			double b1 = Math.sqrt(-d) / (2 * a);

			if (b != 0) {
				System.out.printf("%.2f+%.2fi", a1, b1);
				System.out.println();
				System.out.printf("%.2f-%.2fi", a1, b1);
			} else {
				System.out.printf("0.00+%.2fi", b1);
				System.out.println();
				System.out.printf("0.00-%.2fi", b1);
			}
		} else if (d == 0) {
			double x = -b / (2 * a);
			System.out.printf("%.2f", x);
		}
	}
}

你可能感兴趣的:(java,pat)