用java编写二次方程,创建一个Java程序来解决二次方程

Solving a quadratic equation

I have the following written down so far. I am not sure on how to introduce the second method

public static void main(string args[]){

}

public static double quadraticEquationRoot1(int a, int b, int c) (){

}

if(Math.sqrt(Math.pow(b, 2) - 4*a*c) == 0)

{

return -b/(2*a);

} else {

int root1, root2;

root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);

root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);

return Math.max(root1, root2);

}

}

解决方案

Firstly, your code won't compile--you have an extra } after the start of public static double quadraticEquationRoot1(int a, int b, int c) ().

Secondly, you aren't looking for the correct input types. If you want input of type double, make sure you declare the method appropriately. Also be careful of declaring things as int when they could be doubles (for example, root1 and root2).

Thirdly, I don't know why you have the if/else block--it would be better to simply skip it, and only use the code that is currently in the else part.

Finally, to address your original question: Simply create a separate method and use Math.min() instead of Math.max().

So, to recap in code:

public static void main(string args[]){

}

//Note that the inputs are now declared as doubles.

public static double quadraticEquationRoot1(double a, double b, double c) (){

double root1, root2; //This is now a double, too.

root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);

root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);

return Math.max(root1, root2);

}

public static double quadraticEquationRoot2(double a, double b, double c) (){

//Basically the same as the other method, but use Math.min() instead!

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java编写一元二次方程的步骤如下: 1. 定义三个变量,分别表示一元二次方程的系数 a、b 和 c。 2. 使用公式计算一元二次方程的两个解,公式如下: x1 = (-b + sqrt(b^2-4ac)) / 2a x2 = (-b - sqrt(b^2-4ac)) / 2a 其中,sqrt 表示平方根。 3. 输出结果。 示例代码如下: ``` import java.lang.Math; public class Main { public static void main(String[] args) { // 定义系数 double a = 1.0, b = -3.0, c = 2.0; // 计算解 double x1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a); double x2 = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a); // 输出结果 System.out.println("x1 = " + x1); System.out.println("x2 = " + x2); } } ``` 运行结果如下: ``` x1 = 1.0 x2 = 2.0 ``` ### 回答2: 可以使用Java编写一个求解一元二次方程程序。具体代码如下: ```java import java.util.Scanner; public class QuadraticEquationSolver { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一元二次方程的三个系数(a, b, c):"); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); double discriminant = b * b - 4 * a * c; if (discriminant > 0) { double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); System.out.println("方程有两个实根:"); System.out.println("根1 = " + root1); System.out.println("根2 = " + root2); } else if (discriminant == 0) { double root = -b / (2 * a); System.out.println("方程有一个实根:"); System.out.println("根 = " + root); } else { System.out.println("方程无实根。"); } } } ``` 以上代码通过读取用户输入的系数a、b、c,计算一元二次方程的判别式,并根据判别式的值判断方程的根的情况。如果判别式大于0,则方程有两个实根;如果判别式等于0,则方程有一个实根;如果判别式小于0,则方程无实根。最后,程序输出方程的根或者无实根的信息。 ### 回答3: 以下是用Java编写的一元二次方程求解程序: ```java import java.util.Scanner; public class QuadraticEquationSolver { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一元二次方程的系数:"); System.out.print("a:"); double a = scanner.nextDouble(); System.out.print("b:"); double b = scanner.nextDouble(); System.out.print("c:"); double c = scanner.nextDouble(); double discriminant = b * b - 4 * a * c; if (discriminant > 0) { double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); System.out.println("方程有两个实根:"); System.out.println("根1:" + root1); System.out.println("根2:" + root2); } else if (discriminant == 0) { double root = -b / (2 * a); System.out.println("方程有一个实根:"); System.out.println("根:" + root); } else { System.out.println("方程无实根。"); } } } ``` 以上代码通过输入一元二次方程的系数a、b、c,计算并输出方程的解。程序首先使用`Scanner`类获取用户输入的系数值,然后根据一元二次方程的求根公式计算判别式的值。根据判别式的值,程序分别判断方程有两个实根、一个实根或无实根,并打印出相应的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值