【问题标题】:arrays and static methods java数组和静态方法java
【发布时间】:2013-12-31 15:31:29
【问题描述】:

我有一个关于如何调用数组静态方法的问题。就我而言,我相信我的数组静态方法很好,因为编译器不会抱怨它,但是当我调用它时它会说double cannot be converted to double[]我该如何解决这个问题?任何帮助将不胜感激。以下是我的代码的 sn-p:

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

【问题讨论】:

  • allowedValuesX(x[i], y[j], choice);double[] x, double[] y, int choice。你看得到差别吗?您的签名需要一个数组,但您使用数组中的单个值调用它(又名:单个 double)。

标签: java arrays static-methods


【解决方案1】:

您在调用allowedValuesX 时传递了特定的数组元素:

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

但你应该传递数组本身,以匹配方法的参数类型:

allowedValuesX(x, y, choice);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-29
    • 2013-10-20
    • 1970-01-01
    • 2019-07-06
    • 2015-03-14
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多