我需要在我的Android游戏中使用“need”方法,但是eclipse说没有这样的方法。下面是我的代码:
import java.lang.Math;//in the top of my file
float distance = hypot(xdif, ydif);//somewhere in the code发布于 2012-08-04 18:22:13
首先,你根本不需要在java.lang中导入类型。已经有一个隐式的import java.lang.*;。但是导入类型只是通过其简单的名称来提供该类型;这并不意味着您可以在不指定类型的情况下引用这些方法。您有三个选项:
导入静态java.lang.Math.hypot;// etc
导入静态java.lang.Math.*;
//参见下面的注释: float distance = Math.hypot(xdif,ydif);
还要注意,hypot返回double,而不是float -因此您需要强制转换,或者使distance成为double
// Either this...
double distance = hypot(xdif, ydif);
// Or this...
float distance = (float) hypot(xdif, ydif);发布于 2012-08-04 18:21:16
double distance = Math.hypot(xdif, ydif); 或
import static java.lang.Math.hypot;发布于 2012-08-04 18:22:31
要使用静态方法而不使用它们所在的类,您必须静态地导入它。将您的代码更改为以下代码之一:
import static java.lang.Math.*;
float distance = hypot(xdif, ydif);//somewhere in the code或者这样:
import java.lang.Math;
float distance = Math.hypot(xdif, ydif);//somewhere in the codehttps://stackoverflow.com/questions/11807779
复制相似问题