我不能在Java中使用数学方法

Paw*_*nr1 2 java math android

我需要在我的Android游戏中使用"hypot"方法,但是eclipse说没有这样的方法.这是我的代码:

import java.lang.Math;//in the top of my file
float distance = hypot(xdif, ydif);//somewhere in the code
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 20

首先,您根本不需要导入类型java.lang.import java.lang.*;已经隐含了一些.但是导入一个类型只是通过它的简单名称使该类型可用; 这并不意味着您可以在不指定类型的情况下引用方法.你有三个选择:

还要注意hypot返回double,而不是float- 所以你需要强制转换,或者做distance一个double:

// Either this...
double distance = hypot(xdif, ydif);

// Or this...
float distance = (float) hypot(xdif, ydif);
Run Code Online (Sandbox Code Playgroud)