I can't use math methods in Java
28,881
Solution 1
Firstly, you don't need to import types in java.lang
at all. There's an implicit import java.lang.*;
already. But importing a type just makes that type available by its simple name; it doesn't mean you can refer to the methods without specifying the type. You have three options:
Use a static import for each function you want:
import static java.lang.Math.hypot; // etc
Use a wildcard static import:
import static java.lang.Math.*;
Explicitly refer to the static method:
// See note below float distance = Math.hypot(xdif, ydif);
Also note that hypot
returns double
, not float
- so you either need to cast, or make distance
a double
:
// Either this...
double distance = hypot(xdif, ydif);
// Or this...
float distance = (float) hypot(xdif, ydif);
Solution 2
double distance = Math.hypot(xdif, ydif);
or
import static java.lang.Math.hypot;
Author by
Pawelnr1
Updated on August 05, 2020Comments
-
Pawelnr1 over 3 years
I need to use "hypot" method in my Android game however eclipse says there is no such a method. Here is my code:
import java.lang.Math;//in the top of my file float distance = hypot(xdif, ydif);//somewhere in the code