首页 > 代码库 > HW6.25

HW6.25

技术分享

 

 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         System.out.print("Enter a, b, c: "); 9         double[] equtation = new double[3];10         double[] roots = new double[2];11         for(int i = 0; i < 3; i++)12             equtation[i] = input.nextDouble();13 14         input.close();15 16         if(solveQuadratic(equtation, roots) > 0)17             System.out.println(roots[0] + " " + roots[1]);18     }19 20     public static int solveQuadratic(double[] equ, double[] roots)21     {22         double delta = equ[1] * equ[1] - 4 * equ[0] * equ[2];23         if(delta > 0)24         {25             roots[0] = (-equ[1] + Math.sqrt(delta)) / (2 * equ[0]);26             roots[1] = (-equ[1] - Math.sqrt(delta)) / (2 * equ[0]);27             return 2;28         }29         else if(delta == 0)30         {31             roots[0] = -equ[1] / (2 * equ[0]);32             roots[1] = -equ[1] / (2 * equ[0]);33             return 1;34         }35         else36             return 0;37     }38 }

 

HW6.25