I am having trouble with NAN and Real roots placement. Here's a screenshot of my results:
This is what I am trying to do :
Prompt the user to input the coefficients of the quadratic equation and print out the equation in standard form: ax^2+bx+c=0 (2 pts)
Calculate the roots of the equation. If a=0
, print a message indicating the equation is not a valid quadratic equation. Otherwise, calculate the roots based on the discriminant of the quadratic formula.
When the discriminant is non-negative, print out the real root(s). Note: print the smaller root first. When the discriminant is negative, calculate the real and imaginary parts of the complex roots separately and then print the roots in standard complex 'a+bi' form. Note: print the complex root with the +bi first. (8 pts)
public class Quadratic { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double discriminant = Math.pow(b, 2) - 4 * a * c; System.out.println("a:"); System.out.println("b:"); System.out.println("c:"); System.out.println("Equation: " + a + "x^2+" + b + "x+" + c + "=0"); //If a=0, print a message indicating the equation is not a valid quadratic equation. if (a == 0) { System.out.println("Invalid quadratic equation."); } //When the discriminant is non-negative, print out the real root(s). if (discriminant > 0) { System.out.println("Real root(s):"); double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a); double root2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a); System.out.println("x = " + root2); System.out.println("x = " + root1); } //When the discriminant is negative, calculate the real and imaginary parts of the complex roots separately. if (discriminant < 0) { System.out.println("Complex roots:"); double r1 = -b/(2*a); double r2 = Math.sqrt(-discriminant)/(2*a); System.out.println("x = " + r1 + "+" + r2 + "i"); System.out.println("x = " + r1 + "-" + r2 + "i"); } } }
Use an else
when either this or that is the condition to be followed, and non-negative shall include 0, as:
if (a == 0) {
System.out.println("Invalid quadratic equation.");
} else if (discriminant >= 0) { // non-negative
System.out.println("Real root(s):");
...
} else {
System.out.println("Complex roots:");
...
First problem : you go on to compute and display roots even when the equation is not quadratic ( a == 0
);
Second problem : you excluded the case of discriminant == 0
by taking discriminant < 0
to indicate complex roots and discriminant > 0
to mean real roots.
The correct logic to handle both issues is:
if (a == 0) {
// it's not a quadratic: give message
}
else {
if (discriminant < 0) {
// compute and display complex roots
}
else { // disciminant is zero or greater...
// compute and display real roots
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.