Hey everyone, complete noob here, I'm writing a program for my class about a quadratic equation. Everything in the code seems correct, only java just doesnt seem to care about if/else statements, anyway here is my code:
The program works fine if the determinant is positive and has two solutions, but if it is only one solution, the results read like this:/** * */ package edu.vtc.aav10260.cis2261; import java.util.Scanner; /** * @author andre * */ public class Quadratic { /** * @param args unused */ public static void main(String[] args) { Scanner input = new Scanner(System.in); String equation = "( ax^2 + bx + c = 0)"; System.out.println(equation); System.out.println("Enter the coefficients"); System.out.print("a= "); double a = input.nextDouble(); System.out.print("b= "); double b = input.nextDouble(); System.out.print("c= "); double c = input.nextDouble(); System.out.println(); double determinant = Math.sqrt((b*b)-(4*a*c)); System.out.println(determinant); if (determinant < 0) { System.out.println("There are no solutions"); } else if (determinant == 0) { System.out.println("There is one solution"); double solution = (-b/(2*a)); System.out.println(solution); } else if (determinant > 0); { System.out.println("There are two solutions"); double solution1 = ((-b + determinant)/(2*a)); double solution2 = ((-b - determinant)/(2*a)); System.out.println(solution1); System.out.println(solution2); } } }
( ax^2 + bx + c = 0)
Enter the coefficients
a= 1
b= 2
c= 1
0.0
There is one solution
-1.0
There are two solutions
-1.0
-1.0
And if the determinant is negative, giving no solutions, it looks like this:
( ax^2 + bx + c = 0)
Enter the coefficients
a= 1
b= 2
c= 3
NaN
There are two solutions
NaN
NaN
This makes no sense as my statements explicitly state certain things to do if the determinant is less than, equal to or greater than zero, however the program seems to just print everything out regardless of what the determinant is