2013-12-13 113 views
0

我對如何調用數組靜態方法的問題。就我而言,我相信我的陣列靜態方法是很好的,因爲編譯器不抱怨,但是當我把它這樣做,說:double cannot be converted to double[]我該如何解決這個問題?任何幫助將不勝感激。下面是我的代碼片段:陣列和靜態方法的java

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

'allowedValuesX(x [i],y [j],choice);'vs'double [] x,double [] y,int choice'。你看得到差別嗎?您的簽名期待一個數組,但你從陣列中的單個值調用它(又名:單'double')。 –

回答

3

調用allowedValuesX你逝去的特定數組元素:

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

但是,你應該通過陣列自身,以匹配參數類型的方法:

allowedValuesX(x, y, choice);