java错误“.class预期”

发布于 2024-10-04 09:26:22 字数 928 浏览 2 评论 0原文

我正在尝试调用一个计算java中平均值的方法。但是当我编译时,它总是输出 '.class' Expected 当它到达该行时: System.out.println("平均值:" + Average(double Value[]));

这是我的代码:

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;
}
}

谢谢

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[]));

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2024-10-11 09:26:22

这就是失败的地方:

 "Average: " + Average(double Value[])

double Value[] 位应该是该方法的参数,例如,

"Average: " + Average(Value)

我强烈建议您开始遵循正常的 Java 命名约定,例如使用 PascalCase 命名类、方法和使用驼峰命名法的变量。另外,鉴于您的 Value 变量实际上包含多个值,我会将其复数为 values。当名称选择得当时,您会惊讶地发现代码更容易阅读:)

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 :)

可是我不能没有你 2024-10-11 09:26:22

建议:每当需要数组时,请使用带有 ArrayList 的列表接口。它可以让你避免犯愚蠢的错误。

Suggestion: Use the List Interface with an ArrayList whenever you need an Array. It saves you from making stupid mistakes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文