【问题标题】:Using the pow()-method in Java在 Java 中使用 pow() 方法
【发布时间】:2011-09-30 17:48:20
【问题描述】:

我正在用 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) 无法识别类型 Functions”(Functions 是我的类的名称)。

有谁知道我该如何解决这个问题?我很确定我在正确的轨道上,我只需要让方法正常工作。

任何帮助将不胜感激!

【问题讨论】:

    标签: java pow


    【解决方案1】:

    使用Math.pow(double, double),或静态导入pow

    import static java.lang.Math.pow;
    

    【讨论】:

      【解决方案2】:

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

      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

      【讨论】:

      • 非常感谢!是的,我当然应该在第二个参数中使用 i 而不是 n。非常感谢!
      【解决方案3】:

      那是因为 pow 是 Math(java.lang.Math) 类中的静态方法。您必须改用 Math.pow。

      【讨论】:

        【解决方案4】:

        正如其他人所指出的,您可以通过导入 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;
        
        }
        

        【讨论】:

        • 非常感谢。这很有帮助。干杯!
        【解决方案5】:

        我提出的一个解决方案可以帮助您更清楚地理解一些事情。

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

        【讨论】:

        • @Kristian 我希望这会有所帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-01
        • 2019-05-03
        • 1970-01-01
        相关资源
        最近更新 更多