gpt4 book ai didi

java - 错误: cannot find symbol [compiler error]

转载 作者:行者123 更新时间:2023-12-01 18:33:25 24 4
gpt4 key购买 nike

嘿,我刚刚开始我的第一个 java 编程类(class)。在类里面,我们编写了一个非常基本的代码,用于将调和级数中的数字相加。我收到错误:在 Total+= (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);
}
}

如果有任何答案,我将不胜感激。我一直在努力尝试一个小时。我不敢相信我找不到这么简单的东西。感谢您的帮助。

最佳答案

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

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

关于java - 错误: cannot find symbol [compiler error],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com