Kru*_*hna 6 java if-statement variable-declaration
if(someCondition)
int a=10;//Compilation Error
else if(SomeOtherCondition){
int b=10;//no compilation Error
}
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况.为什么在第一种情况下出现编译错误.如果我放括号,那么没有编译错误,但if语句括号是可选的,如果它是一个语句.
您需要定义int a
in的范围,if statement
它将使用花括号定义{}
.
if(someCondition){
int a=10; // works fine
}else if(SomeOtherCondition){
int b=10; //works fine
}
Run Code Online (Sandbox Code Playgroud)