开发者

Using the pow()-method in Java

开发者 https://www.devze.com 2023-04-09 09:51 出处:网络
I am writing a program in Java in which the user is supposed input an integer, n.My program should then create an array where the entries are [1.25^0], [1.25^1], . . ., [1.25^n].In order to make this

I am writing a program in Java in which the user is supposed input an integer, n. My program should then create an array where the entries are [1.25^0], [1.25^1], . . ., [1.25^n]. In order to make this work I have attempted to use the pow()-method. My code for creating the array is as follows:

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

This is, however, giving me the error message: "the method pow(double, int) is unidentified for the type开发者_JS百科 Functions" (Functions is the name of my class).

Does anyone know how I can fix this? I am pretty sure I am on the right track, I just need to get the method to work properly.

Any help will be greatly appreciated!


Use Math.pow(double, double), or statically import pow as such:

import static java.lang.Math.pow;


Sure, you just need to call Math.pow(...), as it's a static method in the Math class:

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

Note that I've changed it to use i rather than n as the second argument.

You could also get your original code to compile using:

import static java.lang.Math.pow;

in the import statements at the top of your code. See the Java Language Specification, section 7.5.3 for details of how this works.


that would be because pow is a static method in the Math(java.lang.Math) class. You have to use Math.pow instead.


You can solve this problem, as others have noted, with importing of Math.pow or explicitly calling it. However, seeing as you're always using integers as your powers, Math.pow() is a fairly expensive call compared to straight multiplication. I would suggest a method like so. It may give you slightly different results, but it should be sufficient.

/**
 * 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;

}


A solution I made that may help you understand a few things more clearly.

// 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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消