Question:
WRITING A JAVA PROGRAM PLEASE HELP!!!?
bob bob
2011-02-17 06:12:12 UTC
My code is
import java.io.*;
import java.util.*;
public class QuadraticFormula
{
public static void main(String[] args)
{
System.out.println("The form must be ax^2+bx+c to use this program.");

Scanner kbReader=new Scanner(System.in);
System.out.print("Enter A: ");//ax^2
double a= kbReader.nextDouble();

System.out.print("Enter B: ");//bx
double b= kbReader.nextDouble();

System.out.print("Enter C: ");// c
double c= kbReader.nextDouble();

double ab= 2;//number for the second power
double d= Math.pow( (double) b, (double) ab);//raises b two the 2nd power. first part of determinate

double e= b*(-1); //first part of formula. changes b to a negative

double f= 2*a;//divisore of problem

double ga= 4*a;
double g= ga*c;//second part of determinate

double h= d-g;//whole determinate

double i= Math.sqrt( (double) h); //sqrt b^2-4ac

double bc= e+i;//top of equation added
double cd= e-i;//top of equation subtracted

double de= bc/f;//whole equation
double ef= cd/f;//whole equation



System.out.println("The zeros of this equation are "+ de + " and "+ ef + ".");

}
}



When i run the program its out put is -

The form must be ax^2+bx+c to use this program.
Enter A: 1
Enter B: 2
Enter C: 8
The zeros of this equation are NaN and NaN.

Where it says NaN is where it messes up, i wrote all the other stuff in the code. My question is why does it say NaN and how do i fix it. i know NaN means not a number but where is my error and how do i fix it. please help!!!
Three answers:
deonejuan
2011-02-17 07:29:49 UTC
when you init a double, you have to either...



double ab = 2.0;

double ab = 2d;

otherwise you've got Zero or NaN or undefined. So, try that.



kbScanner.nextDouble() will be a type double, so that works. But the problem starts with the line

double i = Math.sqrt( h);

h is -28.0

I'm weak in math, I seem to recall square root must be a positive?





public class QuadraticEquation {



public static void main(String[] args) {

System.out.println("The form must be ax^2+bx+c to use this program.");



// Scanner kbReader = new Scanner(System.in);

// System.out.print("Enter A: ");//ax^2

// double a = kbReader.nextDouble();

//

// System.out.print("Enter B: ");//bx

// double b = kbReader.nextDouble();

//

// System.out.print("Enter C: ");// c

// double c = kbReader.nextDouble();



double a = 1d;

double b = 2d;

double c = 8d;



double ab = 2d;//number for the second power

double d = Math.pow( b, ab);//raises b two the 2nd power. first part of determinate

System.out.println("d: " + d);

double e = b * (-1.0); //first part of formula. changes b to a negative

System.out.println("e: " + e);

double f = 2.0 * a;//divisore of problem

System.out.println("f: " + f);

double ga = 4.0 * a;



double g = ga * c;//second part of determinate

System.out.println("g: " + g);

double h = d - g;//whole determinate

System.out.println("h: " + h);

double i = Math.sqrt( Math.abs(h)); //sqrt b^2-4ac

System.out.println("i: " + i);

double bc = e + i;//top of equation added

double cd = e - i;//top of equation subtracted



double de = bc / f;//whole equation

double ef = cd / f;//whole equation



System.out.println("de: " + de);



System.out.println("The zeros of this equation are " + de + " and " + ef + ".");



}

}
T
2011-02-17 16:30:07 UTC
If you could provide the formula it would be easier to solve the issue. Looking at it now you seem to be casting way to much and you have a lot of unnecessary variables. That seems to either be bad form or just part of the equation.
?
2011-02-17 14:17:32 UTC
Maybe because you got an imaginary answer.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...