gpt4 book ai didi

java - if 子句中的变量声明

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:02:32 25 4
gpt4 key购买 nike

if(someCondition)
int a=10;//Compilation Error
else if(SomeOtherCondition){
int b=10;//no compilation Error
}

为什么会这样。为什么第一种情况会出现编译错误。如果我放上大括号,则不会出现编译错误,但对于 if 语句,如果它是一个语句,大括号是可选的。

最佳答案

您需要在 if 语句 中定义 int a 的范围,它将用花括号 {} 定义。

if(someCondition){
int a=10; // works fine
}else if(SomeOtherCondition){
int b=10; //works fine
}

关于java - if 子句中的变量声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15871379/

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