用JAVA编程求一元二次方程的根,一元二次方程的系数从键盘输入
2个回答
展开全部
import java.util.Scanner;
import org.junit.Test;
public class MyTest {
/**
* 用JAVA编程求一元森历二次方程的根,一元二次方程闷液的系数从键盘输入
*
* 测试用例:
* a b c
* 0
* 1 2 3
* 10 0 -100
* 10 0 100
* 10 50 0
* 10 -50 0
* 1 0 0
*/
@Test
public void test1(){
Double a;
Double b;
Double c;
Scanner s = new Scanner(System.in);
System.out.println("一元二次方程格式为:ax^2+bx+c=0(a≠0,且a,b,c是常数)");
System.out.print("请输入a的值:");
a = s.nextDouble();
while(a==0){
System.out.println("a不能为0,请重新输入:蚂春物");
a = s.nextDouble();
}
System.out.print("请输入b的值:");
b = s.nextDouble();
System.out.print("请输入c的值:");
c = s.nextDouble();
double temp = b*b-4*a*c;
if(temp<0){
System.out.println("该方程无解");
}else if(temp==0){
System.out.println("该方程只有一种解:");
Double answer = ((-b)/(2*a));
System.out.println("x = "+answer);
}else{
System.out.println("该方程有两种解:");
Double answer1 = ((-b)+Math.sqrt(temp))/2*a;
Double answer2 = ((-b)-Math.sqrt(temp))/2*a;
System.out.println("x = "+answer1+" 或 x = "+answer2);
}
}
}
import org.junit.Test;
public class MyTest {
/**
* 用JAVA编程求一元森历二次方程的根,一元二次方程闷液的系数从键盘输入
*
* 测试用例:
* a b c
* 0
* 1 2 3
* 10 0 -100
* 10 0 100
* 10 50 0
* 10 -50 0
* 1 0 0
*/
@Test
public void test1(){
Double a;
Double b;
Double c;
Scanner s = new Scanner(System.in);
System.out.println("一元二次方程格式为:ax^2+bx+c=0(a≠0,且a,b,c是常数)");
System.out.print("请输入a的值:");
a = s.nextDouble();
while(a==0){
System.out.println("a不能为0,请重新输入:蚂春物");
a = s.nextDouble();
}
System.out.print("请输入b的值:");
b = s.nextDouble();
System.out.print("请输入c的值:");
c = s.nextDouble();
double temp = b*b-4*a*c;
if(temp<0){
System.out.println("该方程无解");
}else if(temp==0){
System.out.println("该方程只有一种解:");
Double answer = ((-b)/(2*a));
System.out.println("x = "+answer);
}else{
System.out.println("该方程有两种解:");
Double answer1 = ((-b)+Math.sqrt(temp))/2*a;
Double answer2 = ((-b)-Math.sqrt(temp))/2*a;
System.out.println("x = "+answer1+" 或 x = "+answer2);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询