此处不允许 JAVA 变量声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31230722/
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 Variable declaration not allowed here
提问by TheXDX
I get an error "Variable declaration not allowed here" and I don't know why, I'm new in java and can't find answer :/ As it says, I can't make "int" in "if" but is there a way to create it?
我收到一个错误“这里不允许变量声明”,我不知道为什么,我是 Java 新手,找不到答案:/ 正如它所说,我不能在“if”中创建“int”但是有没有办法创建它?
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;import java.util.Scanner;
public class test{
public static void main(String[] args) throws FileNotFoundException{
File plik = new File("test.txt");
PrintWriter saver = new PrintWriter("test.txt");
int score = 0;
System.out.println("Q: What's bigger");
System.out.println("A: Dog B: Ant");
Scanner odp = new Scanner(System.in);
string odpo = odp.nextLine();
if(odpo.equals("a"))
int score = 1;
else
System.out.println("Wrong answer");
}
}
采纳答案by Luigi Cortese
string
must be changed to String
.
string
必须改为String
.
By writing int score
you're trying to declare a new variable that already exists, which you declared before already. Just remove the int
part and you will get the assignment you want.
通过编写,int score
您试图声明一个已经存在的新变量,您之前已经声明了该变量。只需删除该int
部分,您就会得到您想要的分配。
回答by barunsthakur
As per Java spec, You cannot declare a local variable when there is no scope. While declaring int score = 1
in if, there is no scope. http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html
根据 Java 规范,当没有作用域时,您不能声明局部变量。在声明int score = 1
if 时,没有范围。http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html
A local variable, one of the following
*A local variable declared in a block
*A local variable declared in a for statement
局部变量,以下之一
*在块中
声明的局部变量 *在 for 语句中声明的局部变量
Also you have already declared a variable named score
above. Even if you remove that declaration, you'll get the error because of the above reason.
此外,您已经声明了一个score
上面命名的变量。即使您删除了该声明,由于上述原因,您也会收到错误消息。
回答by Pshemo
Change int score = 1;
to score = 1;
.
更改int score = 1;
为score = 1;
。
Explanation:
解释:
To declare variable we use
要声明变量,我们使用
someType variable;
To assign (or change) value to variable we use
为我们使用的变量分配(或更改)值
variable = value;
We can mix these instruction into one line like;
我们可以将这些指令混合成一行;
someType variable = value;
So when you do
所以当你做
int score = 1;
you first declare variable score
and then assign 1
to it.
您首先声明变量score
,然后分配1
给它。
Problem here is that we can't have two (or more) local variables with same name in same scope. So something like
这里的问题是我们不能在同一范围内有两个(或更多)同名的局部变量。所以像
int x = 1;
int x = 2;
System.out.println(x)
is incorrect because we can't decide which x
we should use here.
是不正确的,因为我们无法决定x
我们应该在这里使用哪个。
Same about
一样的
int x = 1;
{
int x = 2;
System.out.println(x)
}
So if you simply want to change value of already created variable use only assignment, don't include declaration part (remove type information)
因此,如果您只想更改已创建变量的值,请仅使用赋值,不要包含声明部分(删除类型信息)
int x = 1;
//..
x = 2;//change value of x to 2
Now it is time for confusing part - scope. You need to understand that variable have some are in which they can be used. This area is called scope, and is marked with {
}
brackets which surrounds declaration of variable. So if you create variable like
现在是混淆部分 - 范围的时候了。您需要了解变量有一些可以使用的地方。该区域称为作用域,并用{
}
括号标记在变量声明周围。所以如果你创建变量像
{
int x = 1;
System.out.println(x); //we can use x here since we are in its scope
}
System.out.println(x); //we are outside of x scope, so we can't use it here
int x = 2;
System.out.println(x); //but now we have new x variable, so it is OK to use it
So because of that scope limitation declarations in places like
所以因为范围限制声明在像这样的地方
if (condition)
int variable = 2;
else
int variable = 3;
are incorrect because such code is equal to
不正确,因为这样的代码等于
if (condition){
int variable = 2;
}else{
int variable = 3;
}
so this variable couldn't be accessible anywhere.
所以这个变量在任何地方都无法访问。
回答by gauravteg-
Having same name variable declared and initialized in the same scope is not permitted and hence you are seeing the problem.
不允许在同一范围内声明和初始化同名变量,因此您会看到问题。
You should just have it declared once and then reassign new value to it.
您应该只声明一次,然后为其重新分配新值。
int score = 0;
System.out.println("Q: What's bigger");
System.out.println("A: Dog B: Ant");
Scanner odp = new Scanner(System.in);
string odpo = odp.nextLine();
if(odpo.equals("a"))
score = 1; // No need to Declare score again here
else
System.out.println("Wrong answer");
}