我想进一步编程,这是朋友推荐我做的一件事。我正在尝试编写一个二次求解器,但它给出了错误的答案,但是我们都不知道为什么。
我们使用这个等式作为例子:x ^ 2 + 4x + 5 = 0
其解决方案是x = -2 + 1且x = -2-i
eclipse控制台读取这个:
Quadratic equation:
Please enter A:
1
Please enter B:
4
Please enter C:
5
Your first solution is: -3.0
Your second solution is: -5.0
以下是使用的代码:
import java.util.Scanner;
public class Main{
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Quadratic equation: ");
System.out.println("Please enter A: ");
while(!input.hasNextInt()){
@SuppressWarnings("unused")
String broken = input.next();
System.out.println("Please enter A: ");
}
int a = input.nextInt();
System.out.println("Please enter B: ");
while(!input.hasNextInt()){
@SuppressWarnings("unused")
String broken = input.next();
System.out.println("Please enter B: ");
}
int b = input.nextInt();
System.out.println("Please enter C: ");
while(!input.hasNextInt()){
@SuppressWarnings("unused")
String broken = input.next();
System.out.println("Please enter C: ");
}
int c = input.nextInt();
double imaginaryCheck = (b*b)-(4*a*c);
if(imaginaryCheck>0){
double plus = (-b) + Math.sqrt(imaginaryCheck) / (2 * a);
double minus = (-b) - Math.sqrt(imaginaryCheck) / (2 * a);
System.out.format("Your first solution is: %1.2f\n", plus);
System.out.format("Your second solution is: %1.2f", minus);
}else{
double plus = (-b) + Math.sqrt((-1*imaginaryCheck)) / (2 * a);
double minus = (-b) - Math.sqrt((-1*imaginaryCheck)) / (2 * a);
System.out.format("Your first solution is: %1.2fi\n", plus);
System.out.format("Your second solution is: %1.2fi", minus);
}
}
}
某处是否有错误,或者我是否以某种方式弄乱了等式?
答案 0 :(得分:1)
您可能应该包含imaginaryCheck==0
的案例,因为在这种情况下,您只能获得一个解决方案。
你总是忘记了部分的括号,使你使用的实际公式错误
最大的问题:您的假想解决方案计算错误。这不是假想值的计算方法。
正确的应该看起来像这样:
double plusReal = (-b) / (double)(2 * a);
double minusReal = (-b) / (double)(2 * a);
double plusImaginary = Math.sqrt(-1 * imaginaryCheck) / (2 * a);
double minusImaginary = -Math.sqrt(-1 * imaginaryCheck) / (2 * a);
System.out.format("Your first solution is: %1.2f + %1.2fi\n", plusReal, plusImaginary);
System.out.format("Your second solution is: %1.2f + %1.2fi", minusReal, minusImaginary);
你必须分别计算实部和虚部 - 你不能简单地总结它们。这将正确输出:
您的第一个解决方案是:-2.00 + 1.00i
你的第二个解决方案是:-2.00 + -1.00i
if
- 分支应如下所示,并带有更正的括号:
double plus = ((-b) + Math.sqrt(imaginaryCheck)) / (2 * a);
double minus = ((-b) - Math.sqrt(imaginaryCheck)) / (2 * a);
System.out.format("Your first solution is: %1.2f\n", plus);
System.out.format("Your second solution is: %1.2f", minus);
代码应该最终看起来像
if (imaginaryCheck > 0) {
double plus = (-b + Math.sqrt(imaginaryCheck)) / (2 * a);
double minus = (-b - Math.sqrt(imaginaryCheck)) / (2 * a);
System.out.format("Your first solution is: %1.2f\n", plus);
System.out.format("Your second solution is: %1.2f", minus);
} else if (imaginaryCheck == 0) {
double plus = -b / (double) (2 * a);
System.out.format("Your only solution is: %1.2f\n", plus);
} else {
double plusReal = -b / (double) (2 * a);
double plusImaginary = Math.sqrt(-1 * imaginaryCheck) / (2 * a);
System.out.format("Your first solution is: %1.2f + %1.2fi\n", plusReal, plusImaginary);
System.out.format("Your second solution is: %1.2f - %1.2fi", plusReal, plusImaginary);
}
答案 1 :(得分:0)
您需要将计算更改为:
if(imaginaryCheck>0){
double plus = ((-b) + Math.sqrt(imaginaryCheck)) / (2 * a);
double minus = ((-b) - Math.sqrt(imaginaryCheck)) / (2 * a);
System.out.format("Your first solution is: %1.2f\n", plus);
System.out.format("Your second solution is: %1.2f", minus);
}else{
double plus = ((-b) + Math.sqrt((-1*imaginaryCheck))) / (2 * a);
double minus = ((-b) - Math.sqrt((-1*imaginaryCheck))) / (2 * a);
System.out.format("Your first solution is: %1.2fi\n", plus);
System.out.format("Your second solution is: %1.2fi", minus);
}
原因:因为*和/运算符的优先级高于+和 - 。有关详细信息,请参阅link
假设:您对虚部的计算是正确的,即%1.2fi。如果不确定计算本身,那么找到想象解决方案的luk2302解决方案就可以了。