【发布时间】: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