简体   繁体   English

数组和静态方法java

[英]arrays and static methods java

I have a question on how to call an array static method. 我对如何调用数组静态方法有疑问。 In my case, I believe that my array static method is fine because the compiler does not complain about it, but it does when I call it, it says double cannot be converted to double[] How can I fix this? 就我而言,我认为我的数组静态方法很好,因为编译器不会抱怨它,但是在我调用它时确实如此,它说double cannot be converted to double[]如何解决此问题? Any help will be greatly appreciated. 任何帮助将不胜感激。 Below is a snippet of my code: 以下是我的代码片段:

// create allowed x values for calculation static method
public static double[] allowedValuesX(double[] x, double[] y, int choice){
    double allowedVx[] = new double[choice];
    int i = 0;
    for(i = 0; i < allowedVx.length; i++){
        if((Math.pow(x[i], 2) + (Math.pow(y[i], 2))) <= 1){
            allowedVx[i] = x[i];
        }
    }
    return allowedVx;
}
// main method
public static void main(String args[]){
// create Scanner object
    Scanner in = new Scanner(System.in);

    // call to promptUser
    promptUser();

    // create variable for recieving user input
    int choice = in.nextInt();

    double x[] = new double[choice];
    int i = 0;
    for(i = 0; i < x.length; i++){
        x[i] = Math.random();
    }
// call to allowed x values
    allowedValuesX(x[i], y[j], choice);

You are passing specific array elements when calling allowedValuesX : 调用allowedValuesX时,您正在传递特定的数组元素:

allowedValuesX(x[i], y[j], choice);

But you should pass the arrays themselves, to match the parameter types of the method: 但是您应该自己传递数组,以匹配方法的参数类型:

allowedValuesX(x, y, choice);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM