Home >Computer Tutorials >Computer Knowledge >Write a Java program to solve the solution of the quadratic equation: ax^2 + bx + c
The following program passed the jdk5.0 test
import java.util.Scanner;
public class Test {
//Solution to quadratic equation of one variable
private static X fx (int a,int b,int c) throws Exception{
X x = new X();
double m = b*b - 4*a*c;
if(m>=0){
x.x1=(-1*b Math.sqrt(m))/(2*a);
x.x2=(-1*b-Math.sqrt(m))/(2*a);
return x;
}
else throw new Exception("No solution");
}
public static void main(String[] args) {
try{
//Input parameters a, b, c
Scanner cin=new Scanner(System.in);
System.out.print("a=");
int a = cin.nextInt();
System.out.print("b=");
int b = cin.nextInt();
System.out.print("c="");
int c = cin.nextInt();
//calculate
X x=fx(a,b,c);
//show
System.out.println("\n1x1="x.x1 "\nx2="x.x2);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
//Quadratic equation solution set packaging class
class X{
double x1;
double x2;
}
public static main(String[] arg0){
if(arg0== null || arg0.length == 0){
System.out.println("Please enter parameters: a, b, c");
System.exit(0);
}
for(int i=0;i
try {
Float.parseFloat(arg0[i]);
} Catch(Exception e){
System.out.println("Please enter a number");
System.exit(0);
}
}
float a = Float.parseFloat(arg0[0]);
float b = 0;
float c = 0;
if(arg0.length >= 2){
b = Float.parseFloat(arg0[1]);
}
if(arg0.length >= 3){
c = Float.parseFloat(arg0[2]);
}
float tmp = b*b - 4*a*c;
if(tmp
System.out.println("x has no real roots");
System.exit(0);
}
if(a==0 & b==0 & c==0){
System.out.println("x has any real root");
System.exit(0);
}
float result = (-1)*b/(2*a);
if(tmp == 0){
System.out.println("x1=x2=" result);
System.exit(0);
}
float x1 = ((-1)*b java.lang.Math.sqrt(tmp))/(2*a);
float x2 = ((-1)*b-java.lang.Math.sqrt(tmp))/(2*a);
System.out.println("x1=" x1);
System.out.println("x2=" x2);
}
import java.io.*;
public class Test1
{
public static void main(String[] args) throws IOException
{
double a,b,c,d,x,y;
System.out.println ("Solution to cubic equation of one variable: y=ax3 bx2 cx d");
System.out.println ("Please enter a value:");
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
a=Double.parseDouble(in1.readLine());
System.out.println("Please enter b value:");
BufferedReader in2=new BufferedReader(new InputStreamReader(System.in));
b=Double.parseDouble(in2.readLine());
System.out.println ("Please enter the c value:");
BufferedReader in3=new BufferedReader(new InputStreamReader(System.in));
c=Double.parseDouble(in3.readLine());
System.out.println ("Please enter the d value:");
BufferedReader in4=new BufferedReader(new InputStreamReader(System.in));
d=Double.parseDouble(in4.readLine());
System.out.println("Please enter x value:");
BufferedReader in5=new BufferedReader(new InputStreamReader(System.in));
x=Double.parseDouble(in5.readLine());
y=a*Math.pow(x, 3) b*Math.pow(b,2) c*x d;
System.out.println("y value is:" y);
}
}
The above is the detailed content of Write a Java program to solve the solution of the quadratic equation: ax^2 + bx + c. For more information, please follow other related articles on the PHP Chinese website!