2014-01-10 60 views
0

我正在編寫一個程序來查找數組的平均值以及大於該平均值的數字。我試圖用一種方法來寫這一切。但是,由於我被告知我所擁有的是非法表達,所以我在方法聲明中遇到了問題。我究竟做錯了什麼?方法聲明中的錯誤

public class Average { 

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 





public double average(double[] number) { 

    int x = 0; 
    double sum = 0; 
    double[] numberList = new double[10]; //array to hold all numbers 
    double[] largerList = new double[10]; //array to hold numbers greater than the average 
    double[] smallerList = new double[10]; 

    int averageIndex = 0; 
    int largerIndex = 0; 
    int smallerIndex = 0; 

謝謝

回答

0

您不應該在方法中聲明方法。

import java.util.Scanner; 

public class Average { 

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 
} 


public double average(double[] number) { 

    int x = 0; 
    double sum = 0; 
    double[] numberList = new double[10]; //array to hold all numbers 
    double[] largerList = new double[10]; //array to hold numbers greater than the average 
    double[] smallerList = new double[10]; 

    int averageIndex = 0; 
    int largerIndex = 0; 
    int smallerIndex = 0; 
    ... More code ... 
} 

如上所示關閉主方法塊然後聲明一個新的方法。

0

您的代碼片段缺失爲main功能的右括號。

0
Note: A method is like passing control to some other person which will do certain types of action,by declaring it inside a method it's like calling the same method again and again 
In case of Java it has main method and all utility methods to do things for you.In your case 

public class Average(){ 
// your main function starts here 
public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 
    // you can ideally call your average method from here 
    // Your method definition and declaration shouldn't be here 
}//end of main method 

public double average(double[] number) { 

    int x = 0; 
    double sum = 0; 
    double[] numberList = new double[10]; //array to hold all numbers 
    double[] largerList = new double[10]; //array to hold numbers greater than the average 
    double[] smallerList = new double[10]; 

    int averageIndex = 0; 
    int largerIndex = 0; 
    int smallerIndex = 0; 
} 
}//Closing your class Average