Solving A QuadraticEquation by Java
来源:互联网 发布:最好的漫画软件 编辑:程序博客网 时间:2024/05/01 04:39
Purpose:This program solves for the rootsof a quadratic equation of the forma*x*x+b*x+c=0. It calculates theanswers regardless of the type ofroots that the equation possesses.import java.io.*;public class QuadraticEquation{//Define the main method.public static void main(String[] args) throws IOException{//Declare variables,and define each variable.double a;double b;double c;double discriminant;double imag_part;double real_part;double x1;double x2;//Create a buffered reader.BufferedReader inl=new BufferedReader(new InputStreamReader(System.in));//Prompt the user for the coefficients of the equation.System.out.println("This program solves for the roots of quadratic equation.");System.out.println("Please input the coefficients of quadratic equation:");System.out.print("Enter the coefficients A: ");a=Double.parseDouble(inl.readLine());System.out.print("Enter the coefficients B: ");b=Double.parseDouble(inl.readLine());System.out.print("Enter the coefficients C: ");c=Double.parseDouble(inl.readLine());System.out.println("The quadratic equation you inputed is:");System.out.println(a+"*x*x + "+b+"*x + "+c+" = 0");//Caluate discriminantdiscriminant=b*b-4*a*c;//Solve for the roots,depending on the discriminant.if(discriminant>0){//Two real rootsx1=(-b+Math.sqrt(discriminant))/(2*a);x2=(-b-Math.sqrt(discriminant))/(2*a);System.out.println("This equation has two real roots:");System.out.println("X1 = "+x1+" , X2 = "+x2);}elseif(discriminant==0){//One repeated rootx1=(-b)/(2*a);System.out.println("This equation has repeated real roots:");System.out.println("X1 = X2 = "+x1);}else{//Two complex rootreal_part=(-b)/(2*a);imag_part=Math.sqrt(Math.abs(discriminant))/(2*a);System.out.println("This equation has two Complex roots:");System.out.println("X1 = "+real_part+"+"+imag_part+"i");System.out.println("X2 = "+real_part+"-"+imag_part+"i");}}}
- Solving A QuadraticEquation by Java
- solving
- Problems Solving--Q&&A(Period One)
- Problems Solving--Q&&A(Period 2)
- Java, Java, Java, Object-Oriented Problem Solving
- Reverse words in a string by java
- Data Abstraction and Problem solving with Java
- solution for solving OutOfMemoryError: Java heap space
- codeforces 822B(Crossword solving) Java
- Practical NoSQL - Solving a Real Problem with MongoDB and Redis
- Solving non-linear problems using a kernel SVM
- the complexity of solving a l0-norm optimization problem
- CodeForces 825A Multi-judge Solving(阅读题)
- Programming a Spider in Java (翻译by:shuidao)
- A Java Fork/Join Framework by Doug Lea
- tomcat Caused by: java.net.SocketException: Not a multicast address
- Sort a Map<Key, Value> by values (Java)
- Java 8 Stream – Read a file line by line
- 为什么Linux的fdisk分区时First Sector是63或2048?
- 美国十大最幸福科技公司:谷歌第四 苹果落榜
- Hibernate三种状态(Transient、Persistent、Detached)各种保存(save,persist,update,saveOrUpdte,merge,flush,lock)区别
- 内存数据库的基本原理与内存数据库的应用
- AutoIT 访问网页
- Solving A QuadraticEquation by Java
- 2013科目一理论考试
- Maven最佳实践:划分模块
- 2013年4月18日 23:37:54
- HttpServlet详解
- linux中wait与waitpid的差别
- 微软“MSDN”原版资源的“Share MSDN”最新中、英文硬盘版和网络版
- Android高级第十讲之AIDL与JNI
- js