java:解一元二次方程

//日常脑残的我...

两个解一个解无解

import java.util.Scanner;
public class thirdegmath {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("enter a,b,c:");
		int a=input.nextInt();
		int b=input.nextInt();
		int c=input.nextInt();
		System.out.println(a+"+x²+"+ b +"x+ "+c +" = 0");
		double t=b*b-4*a*c;
		if(t>0) {
			double r1=(Math.sqrt(t)-b)*1.0/(2*a);
			double r2=(-(Math.sqrt(t))-b)*1.0/(2*a);
			System.out.println(r1+"  and  "+r2);
		}
		else if(t==0)
		System.out.println((Math.sqrt(t)-b)*1.0/(2*a));
		else
			System.out.println("No solution.");
	}
}

 

 

 

你可能感兴趣的:(java基础篇)