2014-01-10 3 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 ... 
} 

AS, показанный выше, закрывает основной блок метода, а затем объявляет новый метод.

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 
Смежные вопросы