This question already has an answer here:
这个问题在这里已有答案:
- Misplaced semicolon in for loop 5 answers
错误的分号在for循环5答案中
I am an absolute beginner to programming and I've started with java. I wrote this code and I just don't know what's wrong with it.
我是编程的绝对初学者,我从java开始。我写了这段代码,我只是不知道它有什么问题。
public class multiples3and5 {
public static void main(String[] args) {
for (int mult3 = 0; mult3 < 1000; mult3 += 3);
System.out.println(mult3);
}
}
I keep getting this error on my terminal:
我一直在终端上收到此错误:
multiples3and5.java:7: error: cannot find symbol
System.out.println(mult3);
^
symbol: variable mult3
location: class multiples3and5
1 error
3 个解决方案
#1
1
It is simply because you are using a semicolon after for loop..The semicolon will end the for loop execution and since your variable mult3 is outside this for loop the declaration is not known to Sopln statement
这只是因为你在for循环后使用分号。分号将结束for循环执行,并且因为你的变量mult3在for循环之外,所以Sopln语句不知道声明
Remove semicolon
for (int mult3 = 0; mult3 < 1000; mult3 += 3)
#2
1
Your line no 5 is wrong. Try like this..
你的第5行错了。试试这样..
for (int mult3 = 0; mult3 < 1000; mult3 += 3)
Remove the last semi-colon.If you put it your mult3 is local and not accessible out of the scope.
删除最后一个分号。如果你把它放在你的mult3是本地的,不能访问范围。
if you want to find the sum of your multiples of 3(as stated in the comments) it is very simple. Declare a variable,initialize it to 0 and the add your loop variable at each iteration.
如果你想找到3的倍数之和(如评论中所述),这很简单。声明一个变量,将其初始化为0并在每次迭代时添加循环变量。
int sum=0;
for (int mult3 = 0; mult3 < 1000; mult3 += 3)
{
sum+=mult3;
}
System.out.println(sum);
And that is it.
就是这样。
#3
1
You didn't start your code block properly. At the end of the for
loop declaration, you put a semicolon instead of an opening curly bracket {
. Without code, this just looped through and removed the mult3
variable from the scope, because it was declared for the loop.
您没有正确启动代码块。在for循环声明的末尾,你放了一个分号而不是一个开头的花括号{。没有代码,这只是循环并从范围中删除了mult3变量,因为它是为循环声明的。
This is the fix:
这是修复:
public class multiples3and5 {
public static void main(String[] args){
for(int mult3 = 0; mult3 < 1000; mult3 += 3){
System.out.println(mult3);
}
}
}
#1
1
It is simply because you are using a semicolon after for loop..The semicolon will end the for loop execution and since your variable mult3 is outside this for loop the declaration is not known to Sopln statement
这只是因为你在for循环后使用分号。分号将结束for循环执行,并且因为你的变量mult3在for循环之外,所以Sopln语句不知道声明
Remove semicolon
for (int mult3 = 0; mult3 < 1000; mult3 += 3)
#2
1
Your line no 5 is wrong. Try like this..
你的第5行错了。试试这样..
for (int mult3 = 0; mult3 < 1000; mult3 += 3)
Remove the last semi-colon.If you put it your mult3 is local and not accessible out of the scope.
删除最后一个分号。如果你把它放在你的mult3是本地的,不能访问范围。
if you want to find the sum of your multiples of 3(as stated in the comments) it is very simple. Declare a variable,initialize it to 0 and the add your loop variable at each iteration.
如果你想找到3的倍数之和(如评论中所述),这很简单。声明一个变量,将其初始化为0并在每次迭代时添加循环变量。
int sum=0;
for (int mult3 = 0; mult3 < 1000; mult3 += 3)
{
sum+=mult3;
}
System.out.println(sum);
And that is it.
就是这样。
#3
1
You didn't start your code block properly. At the end of the for
loop declaration, you put a semicolon instead of an opening curly bracket {
. Without code, this just looped through and removed the mult3
variable from the scope, because it was declared for the loop.
您没有正确启动代码块。在for循环声明的末尾,你放了一个分号而不是一个开头的花括号{。没有代码,这只是循环并从范围中删除了mult3变量,因为它是为循环声明的。
This is the fix:
这是修复:
public class multiples3and5 {
public static void main(String[] args){
for(int mult3 = 0; mult3 < 1000; mult3 += 3){
System.out.println(mult3);
}
}
}