HW6.25

时间:2023-03-08 15:41:43

HW6.25

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter a, b, c: ");
         double[] equtation = new double[3];
         double[] roots = new double[2];
         for(int i = 0; i < 3; i++)
             equtation[i] = input.nextDouble();

         input.close();

         if(solveQuadratic(equtation, roots) > 0)
             System.out.println(roots[0] + " " + roots[1]);
     }

     public static int solveQuadratic(double[] equ, double[] roots)
     {
         double delta = equ[1] * equ[1] - 4 * equ[0] * equ[2];
         if(delta > 0)
         {
             roots[0] = (-equ[1] + Math.sqrt(delta)) / (2 * equ[0]);
             roots[1] = (-equ[1] - Math.sqrt(delta)) / (2 * equ[0]);
             return 2;
         }
         else if(delta == 0)
         {
             roots[0] = -equ[1] / (2 * equ[0]);
             roots[1] = -equ[1] / (2 * equ[0]);
             return 1;
         }
         else
             return 0;
     }
 }