错误:找不到符号[编译错误]

时间:2014-04-17 04:38:57

标签: java compiler-errors

嘿,我刚刚用java开始我的第一个编程课程。在课堂上,我们制作了一个非常基本的代码,用于将谐波系列中的数字相加。我收到一个错误:在总行+ =(1.0 /(x + 1))上找不到x的符号。

以下是代码:

public class Group1
{
public static void main(String[]args)
    {
        foo(3);
    }
public static void foo(int n)
    {
    double total = 0;
    for (int x = 0; x < n; x++);
        {
        total+= (1.0/(x+1));
        }
    System.out.println(total);
    }
}

我很感激任何答案。我一直在努力尝试一小时。我无法相信我找不到这么简单的东西。谢谢你的帮助。

3 个答案:

答案 0 :(得分:3)

删除for循环末尾的;。通过在末尾添加半冒号,您将限制变量x的范围

for (int x = 0; x < n; x++); // <--- remove this semi colon

答案 1 :(得分:2)

删除for语句末尾的半冒号。基本上在你的代码范围内,变量只在for循环内部,因为你通过放置一个半冒号来关闭for循环,x在此之后就消失了。

答案 2 :(得分:-1)

for (int x = 0; x < n; x++);更改为for (int x = 0; x < n; x++)

相关问题