# Need some help.

• May 23rd, 2013, 02:11 PM
PhillipBC
Need some help.
hello everyone,

I just started to do java and I'm suppose to solve an quadratic equation ax^2+bx+c=0.

So everything works good, and im trying to put some restrictions on the equation like if a=0 do this , if square root=0 do this if both do this.

the restriction works, the only problem is that every time I run the program both message shows off. What should I do?

Code :

```System.out.println("Finding the solution to ax^2+bx+c=0"); System.out.println("Write a value for a"); double a = input.nextDouble();   System.out.println("Write a value for b"); double b = input.nextDouble(); System.out.println("Write a value for c"); double c = input.nextDouble(); double square = (Math.sqrt(b*b-4*a*c)); double x1 = (-b+square)/(2*a); double x2 = (-b-square)/(2*a); double xx1 = (-b+square); double xx2 = (-b-square);         if (square > 0) { System.out.println("x1= " +x1+ " and x2= "+x2); }   else { System.out.println("Square root is 0 making x1 and x2 =" +-b/2*a); }   if (a>0) { System.out.println("x1 = "+x1+ " and x2 = "+x2); } else { System.out.println("x1 = "+xx1+ " and x2 = "+xx2); }   } }```

Thank you.
• May 23rd, 2013, 02:22 PM
jbarke12
Re: Need some help.
The reason it is printing twice is because you do your check on the square and then on a instead of doing them together.
• May 23rd, 2013, 03:06 PM
PhillipBC
So I just do them together in the same if or use else if ?
• May 23rd, 2013, 03:15 PM
jbarke12
Re: Need some help.
I would. Basically start with your if statement with the square root. Within that block define what happens if a is above 0 and if a is 0 then do the same thing in the else part of your block for the squareroot
• May 24th, 2013, 09:38 PM
PhillipBC
Thank you