在这里,我需要编写一个名为powArray的方法,该方法采用双精度数组a并返回一个包含a平方元素的新数组。推广它以接受第二个参数并将a的元素提高到给定的幂。
我尝试编写它,但结果为0,有人可以写它并解释它如何工作,所以我将来可以写它。

public class Task {
    public static void main(String[] args) {

    }

    public static double powArray (double a[]){
        for (int i = 1; i < a.length; i++) {
            a[i] = Math.pow(a[i], 2.0);
            System.out.print(a[i]);
            return powArray(a);
        }
        return -1;
    }
}


另外,编译后什么也没有,只清除没有任何数字的控制台,等等。

最佳答案

添加到Sandeep Kakote的答案中:

 public static void main(String[] args) {
            // TODO Auto-generated method stub
            double[] squareArry = powArray(new double[]{10,20,30},3);

        }

        public static double[] powArray (double a[],int z){
            double[] b = new double[a.length];
            for (int i = 0; i < a.length; i++) {
                b[i] = Math.pow(a[i], z);
                System.out.print(b[i]);
            }
            return b;
        }

关于java - 创建数组方法java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43832451/

10-08 22:42
查看更多