java error ".class expected"
I am trying to call a method which c开发者_如何学Pythonalculate the average value in java. But when I compile it always output '.class' expected
when it came to the line:
System.out.println("Average: " + Average(double Value[]));
Here's my code:
public class q2
{
public static void main(String[] args) throws IOException
{
new q2().InputValue();
}
public void InputValue() throws IOException
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
double[] Value = new double[10];
for (int i = 0; i < 10; i++)
{
System.out.println("Please enter a value: ");
Value[i] = Double.parseDouble(br.readLine());
}
System.out.println("Average: " + Average(double Value[]));
}
public double Average(double Value[])
{
double average = 0;
for (int n = 0; n < 10; n++)
{
average = average + Value[n];
}
average = average / 10;
return average;
}
}
Thanks
This is the bit that's failing:
"Average: " + Average(double Value[])
The double Value[]
bit should be an argument for the method, e.g.
"Average: " + Average(Value)
I would strongly recommend that you start following normal Java naming conventions, e.g. naming classes with PascalCase, methods and variables with camelCase. Also, given that your Value
variable actually holds multiple values, I'd pluralize it to values
. You'd be amazed at how much easier code is to read when the names are chosen well :)
Suggestion: Use the List Interface with an ArrayList whenever you need an Array. It saves you from making stupid mistakes.
精彩评论