[Java] 求方程aX^2+bX+c=0的实数解 →→→→→进入此内容的聊天室

来自 , 2019-10-02, 写在 Java, 查看 125 次.
URL http://www.code666.cn/view/df12ecd0
  1. package hwoneweek;
  2. //求方程aX^2+bX+c=0的实数解。要求:a、b、c的值从键盘输入
  3.  
  4. import java.util.Scanner;
  5. public class xcheng{
  6.         public static void main(String[] args){
  7.                 double x = 0;
  8.                 double i ,i1 = 0;
  9.                 System.out.println("求方程aX^2+bX+c=0的实数解,请按提示输入!");  
  10.                 Scanner input = new Scanner(System.in);
  11.                 System.out.println("请输入a:");     
  12.                 double a = input.nextDouble();
  13.                 System.out.println("请输入b(注意a和b不能同时为0):");
  14.                 double b = input.nextDouble();
  15.                 System.out.println("请输入c:");             
  16.                 double c = input.nextDouble();
  17.                 i = b*b-4*a*c; 
  18.                 if(a == 0){
  19.                         if(b == 0)
  20.                                 System.out.println("a和b同时为0,输入错误!!!");
  21.                         else{
  22.                                 x = c/b;
  23.                                 System.out.println("x="+x);
  24.                         }
  25.                 }                      
  26.                 else{
  27.                         if(i<0)
  28.                                 System.out.println("方程无实数解!");
  29.                         else{
  30.                                 i1 = Math.sqrt(i);
  31.                                 System.out.println("方程的实数解为:");
  32.                                 x = (-b+i1)/(2*a);
  33.                                 System.out.println("x1="+x);
  34.                                 x = (-b-i1)/(2*a);
  35.                                 System.out.println("x2="+x);
  36.                         }                      
  37.                 }                                              
  38.         }                                      
  39. }
  40.        
  41.        
  42.        
  43.  

回复 "求方程aX^2+bX+c=0的实数解"

这儿你可以回复上面这条便签

captcha