1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter a, b, c: "); 10 double a = input.nextDouble(); 11 double b = input.nextDouble(); 12 double c = input.nextDouble(); 13 14 input.close(); 15 16 double delta = b * b - 4 * a * c; 17 18 if(delta > 0) 19 { 20 double root1 = (-b + Math.pow(delta, 0.5)) / (2 * a); 21 double root2 = (-b - Math.pow(delta, 0.5)) / (2 * a); 22 System.out.println("The roots are " + root1 + " and " + root2); 23 } 24 else if(delta == 0) 25 { 26 double root = -b / (2 * a); 27 System.out.println("The root is " + root); 28 } 29 else 30 System.out.println("The equation has no real roots."); 31 } 32 }
时间: 2024-10-04 01:49:07