java错误“.class预期”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4285861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java error ".class expected"
提问by Tom
I am trying to call a method which calculate 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[]));
我正在尝试调用一种在 java 中计算平均值的方法。但是当我编译它'.class' expected
时,它总是输出到这一行: 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
谢谢
回答by Jon Skeet
This is the bit that's failing:
这是失败的一点:
"Average: " + Average(double Value[])
The double Value[]
bit should be an argument for the method, e.g.
该double Value[]
位应该是该方法的参数,例如
"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 :)
我强烈建议您开始遵循正常的 Java 命名约定,例如用 PascalCase 命名类,用驼峰命名法命名方法和变量。另外,鉴于您的Value
变量实际上包含多个值,我会将其复数为values
. 当名称选择得当时,您会惊讶于代码阅读起来容易得多:)
回答by Kajetan Abt
Suggestion: Use the List Interface with an ArrayList whenever you need an Array. It saves you from making stupid mistakes.
建议:在需要数组时使用带有 ArrayList 的 List 接口。它可以避免您犯愚蠢的错误。