以下程序在jdk5.0测试通过
import java.util.Scanner;
public class Test {
//一元二次方程式解法
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("无解");
}
public static void main(String[] args) {
try{
//输入参数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();
//计算
X x=fx(a,b,c);
//显示
System.out.println("n1x1="+x.x1+"nx2="+x.x2);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
//一元二次方程解集包装类
class X{
double x1;
double x2;
}
public static main(String[] arg0){
if(arg0== null || arg0.length == 0){
System.out.println("请输入参数:a,b,c");
System.exit(0);
}
for(int i=0;i
try {
Float.parseFloat(arg0[i]);
} Catch(Exception e){
System.out.println("请输入数字");
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无实数根");
System.exit(0);
}
if(a==0 & b==0 & c==0){
System.out.println("x有任意实数根");
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("一元三次方程解:y=ax3+bx2+cx+d");
System.out.println("请输入a值:");
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
a=Double.parseDouble(in1.readLine());
System.out.println("请输入b值:");
BufferedReader in2=new BufferedReader(new InputStreamReader(System.in));
b=Double.parseDouble(in2.readLine());
System.out.println("请输入c值:");
BufferedReader in3=new BufferedReader(new InputStreamReader(System.in));
c=Double.parseDouble(in3.readLine());
System.out.println("请输入d值:");
BufferedReader in4=new BufferedReader(new InputStreamReader(System.in));
d=Double.parseDouble(in4.readLine());
System.out.println("请输入x值:");
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值为:"+y);
}
}
以上是编写一Java程序来求解一元二次方程:ax^2 + bx + c的解的详细内容。更多信息请关注PHP中文网其他相关文章!