The compiler reports the line with the call to Math.pow()
as an error. I'm making a program that computes the solution to a quadratic by using the quadratic formula with inputted variables for class. Arrow for convenience
import java.util.*;
public class QuadraticFormula {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a: ");
int a = input.nextInt();
System.out.println("Enter b: ");
int b = input.nextInt();
System.out.println("Enter c: ");
int c = input.nextInt();
→ int discriminant = (Math.pow(b, 2) - 4*a*c);
input.close();
}
}
Cast it as an int:
This is because Math.pow() expects 2 doubles and returns a double. So the Runtime will up-cast
b
and2
fromint
todouble
then return adouble
. Then it will up-cast4
,a
, andc
todouble
s. The result is adouble
. It must be cast back down toint
. This is safe in this case because we know all input values areint
s.