请考虑以下未格式化的嵌套if-else Java代码
if (condition 1)
if (condition 2)
action 1;
else
action 2;
我的问题是:根据Java语言规范,如果else分支适用该怎么办?
通过手工重新格式化和添加大括号,这两个中的哪一个是正确的?
第1块:
if (condition 1) {
if (condition 2) {
action 1;
} else
action 2;
}
}
第2块:
if (condition 1) {
if (condition 2) {
action 1;
}
}
else {
action 2;
}
解决方法:
The Java programming language, like C and C++ and many programming languages before them, arbitrarily decrees that an else clause belongs to the innermost if to which it might possibly belong.