我的方法没有运行很多错误

时间:2013-12-20 20:50:06

标签: java

public static double calcAverage (int [] high)
{
double sum = 0;
double result;
for (int i=0; i < high.length; i++)
{
sum = sum + high[i];
}
result = sum / high.length;
// return results
return result;
}



error: class, interface, or enum expected
            public static double calcAverage ()
                          ^
error: class, interface, or enum expected
            double result;
            ^
error: class, interface, or enum expected
            for (int i=0; i < high.length; i++)
            ^
error: class, interface, or enum expected
            for (int i=0; i < high.length; i++)
                          ^
error: class, interface, or enum expected
            for (int i=0; i < high.length; i++)
                                           ^
error: class, interface, or enum expected
            }
            ^
error: class, interface, or enum expected
            System.out.println(result);
            ^
error: class, interface, or enum expected
            return result;
            ^
error: class, interface, or enum expected
            }
            ^
9 errors

Tool completed with exit code 1

1 个答案:

答案 0 :(得分:2)

试试这个:

public class NewJavaProgram {

    public static double calcAverage (int [] high) {

        double sum = 0;
        double result;

        for (int i=0; i < high.length; i++) {
            sum = sum + high[i];
        }
        result = sum / high.length;
        // return results
        return result;
    }

    public static void main(String [] args) {
        int [] i=new int[10];
        System.out.println(NewJavaProgram.calcAverage(i));
    }

}