2

私のAndroidゲームでは「hypot」メソッドを使用する必要がありますが、eclipseはそのようなメソッドはないと言っています。これが私のコードです:

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

4 に答える 4

21

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);
于 2012-08-04T10:22:13.353 に答える
2
double distance = Math.hypot(xdif, ydif);  

or

import static java.lang.Math.hypot;
于 2012-08-04T10:21:15.817 に答える
0

To use static methods without the class they are in, you have to import it statically. Change your code to either this:

import static java.lang.Math.*;
float distance = hypot(xdif, ydif);//somewhere in the code

or this:

import java.lang.Math;
float distance = Math.hypot(xdif, ydif);//somewhere in the code
于 2012-08-04T10:22:31.393 に答える
0

1.まず、java.lang.* が既に含まれているため、インポートする必要はありません。

2. Math クラスのメソッドにアクセスするには、次のようにします...

-クラス名とドット演算子を使用して静的メソッドにアクセスします。

Math.abs(-10);

-静的メソッドに直接アクセスしてから、以下のようにインポートする必要があります。

import static java.lang.Math.abs;

abs(-10);

于 2012-08-04T10:39:18.487 に答える