其他分享
首页 > 其他分享> > 计算方程式的解

计算方程式的解

作者:互联网

ax^2 + bx + c = 0

import javax.swing.JOptionPane;
import java.util.*;
class Main {
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a,b,c: ");
    double a = input.nextDouble();
    double b = input.nextDouble();
    double c = input.nextDouble();
    double r1 = 0;
    double r2 = 0;
    if((b*b - 4*a*c) > 0){
      r1 = (-b + Math.pow((b*b - 4*a*c),0.5)) / (2 * a);
      r2 = (-b - Math.pow((b*b - 4*a*c),0.5)) / (2 * a);
      System.out.println("The roots are " + r1 + " and " + r2);
    }else if ((b*b - 4*a*c) == 0){
      r1 = (-b) / (2 * a);
      System.out.println("The root is " + r1);
    }else{
      System.out.println("The equation has no real roots");
    }
  }

}

标签:r1,double,方程式,System,计算,input,nextDouble,out
来源: https://www.cnblogs.com/doudou-20123/p/16257928.html