Java简单题-----计算一元二次方程的根

计算一元二次方程的根:

import java.util.*;

public class theSolutionOfEquation {
	public static void main(String [] args) {
		Scanner in = new Scanner(System.in);
		System.out.print("输入a:");
		int a = in.nextInt();
		System.out.print("输入b:");
		int b = in.nextInt();
		System.out.print("输入c:");
		int c = in.nextInt();
		if(a == 0) {//当a是0的时候,按照一元方程计算
			if(b == 0) {
				System.out.printf("等式不成立");
			} else {
				System.out.println("根为:"+(-c / b));
			}
		} else {
			double data = b*b - 4*a*c;
			if(data < 0) {//有虚根
				data = Math.sqrt(-data);
				System.out.println("两个跟分别为x1 = "+ (-b/2) +"+"+ (data / 2) + "i");
				System.out.println("和x2 = " + (-b/2) + (-data / 2) + "i");
			} else {
				data = Math.sqrt(data);
				if(data == 0) {
					System.out.println("两个跟分别为x1 = " + (-b/2));
					System.out.println("和x2 = " + (-b/2));
				} else {
					System.out.println("两个跟分别为x1 = " + (-b/2) + data / 2);
					System.out.println("和x2 = " + (-b/2) + (-data / 2));
				}
			}
		}
	}
}

你可能感兴趣的:(Java简单题-----计算一元二次方程的根)