有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用pow()方法

我正在用Java编写一个程序,其中用户应该输入一个整数n。然后,我的程序应该创建一个数组,其中的条目是[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)未被识别”(函数是我的类的名称)

有人知道我怎么解决这个问题吗?我很确定我的思路是正确的,我只需要让方法正常工作

任何帮助都将不胜感激


共 (5) 个答案

  1. # 2 楼答案

    当然,您只需要调用Math.pow(...),因为它是^{}类中的静态方法:

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

    注意,我已将其更改为使用i而不是n作为第二个参数

    您还可以使用以下方法编译原始代码:

    import static java.lang.Math.pow;
    

    在代码顶部的导入语句中。请参阅Java语言规范section 7.5.3,了解其工作原理的详细信息

  2. # 3 楼答案

    这是因为pow是Math(java.lang.Math)类中的静态方法。你必须使用数学。而是战俘

  3. # 4 楼答案

    我提出的解决方案可能会帮助你更清楚地理解一些事情

    // 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);
        }
    }
    
  4. # 5 楼答案

    正如其他人所指出的,通过导入数学,您可以解决这个问题。pow或明确地称之为pow。然而,鉴于你总是使用整数作为你的幂,数学。与直接乘法相比,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;
    
    }