3

私は、ユーザーが整数nを入力することになっているJavaでプログラムを書いています。次に、プログラムは、エントリが[1.25 ^ 0]、[1.25 ^ 1] 、.である配列を作成する必要があります。。。、[1.25^n]。この作業を行うために、私はpow()メソッドを使用しようとしました。配列を作成するための私のコードは次のとおりです。

for (int i = 0; i < n; i++) {
    functionG[i] = pow(1.25, n); }

ただし、これはエラーメッセージを表示します:「メソッドpow(double、int)はタイプFunctionsに対して識別されていません」(Functionsは私のクラスの名前です)。

誰かが私がこれを修正する方法を知っていますか?私は正しい方向に進んでいると確信しています。メソッドを正しく機能させる必要があります。

どんな助けでも大歓迎です!

4

5 に答える 5

12

Math.pow(double、double)を使用するか、静的にインポートpowします。

import static java.lang.Math.pow;
于 2011-09-30T17:50:01.557 に答える
5

確かに、それはクラスMath.pow(...)の静的メソッドなので、呼び出す必要があります。Math

for (int i = 0; i < n; i++) {
    functionG[i] = Math.pow(1.25, i); 
}

2番目の引数としてiではなく使用するように変更したことに注意してください。n

次を使用して、元のコードをコンパイルすることできます。

import static java.lang.Math.pow;

コードの上部にあるimportステートメントで。これがどのように機能するかの詳細については、Java言語仕様のセクション7.5.3を参照してください。

于 2011-09-30T17:50:07.377 に答える
1

これは、powがMath(java.lang.Math)クラスの静的メソッドであるためです。代わりにMath.powを使用する必要があります。

于 2011-09-30T17:50:30.353 に答える
1

他の人が指摘したように、Math.pow をインポートするか明示的に呼び出すことで、この問題を解決できます。ただし、累乗として常に整数を使用しているため、Math.pow() は単純な乗算に比べてかなり高価な呼び出しです。などの方法を提案します。わずかに異なる結果が得られる場合がありますが、十分なはずです。

/**
 * Make a double[] that gives you each power of the original
 * value up to a highestPow.
 */
double[] expandedPow(double orig, int highestPow) {

    if(highestPow < 0) return new double[0];
    if(highestPow == 0) return new double[] { 0.0 };
    double[] arr = new double[highestPow + 1];
    arr[0] = 0.0;
    arr[1] = orig;
    for(int n = 2; n < arr.length; n++) {
        arr[n] = arr[n-1] * orig;
    }
    return arr;

}
于 2011-09-30T18:01:14.993 に答える
0

いくつかのことをより明確に理解するのに役立つ可能性がある、私が作成したソリューション。

// Make it ready for the loop, no point calling Math.pow() every loop - expensive
import static java.lang.Math.pow;

public class MyPattern {

    public void showTree(int treeDepth) {

        // Create local method fields, we try to avoid doing this in loops
        int depth = treeDepth;
        String result = "", sysOutput = "";

        // Look the depth of the tree
        for( int rowPosition = 0 ; rowPosition < depth ; rowPosition++ ) {
            // Reset the row result each time
            result = "";

            // Build up to the centre (Handle the unique centre value here)
            for( int columnPosition = 0 ; columnPosition <= rowPosition ; columnPosition++ )
                result += (int) pow(2, columnPosition) + " ";

            // Build up from after the centre (reason we -1 from the rowPosition)
            for ( int columnPosition = rowPosition - 1 ; columnPosition >= 0 ; columnPosition-- )
                result += (int) pow(2, columnPosition) + " ";

            // Add the row result to the main output string
            sysOutput += result.trim() + "\n";
        }

        // Output only once, much more efficient
        System.out.print( sysOutput );
    }

    // Good practice to put the main method at the end of the methods
    public static void main(String[] args) {
        // Good practice to Create Object of itself
        MyPattern test = new MyPattern();

        // Call method on object (very clear this way)
        test.showTree(5);
    }
}
于 2013-11-17T22:30:54.090 に答える