【发布时间】:2017-01-09 01:16:27
【问题描述】:
int a; // why can't I put this in the condition itself
if((a = readData()) > 0){
// do something..
}
我想知道为什么 java 和 javascript 不允许我在 (condition) 中声明我的变量。我知道这与变量声明必须是第一件事有关-除了for循环-但根本原因是什么? In C++ it seems like they can do it。我不知道 C++,所以如果我误解了,我深表歉意。
【问题讨论】:
-
Java 与 Javascript 无关,而且该语言根本不允许您在
if中声明变量。 -
@ElliottFrisch 我提到了这两种语言,因为这是我知道的两种语言,我希望它们的根本原因是相同的。抱歉,如果不清楚。我的问题是他们为什么不允许这样做?
-
我想你需要问问 James Gosling 为什么他不包含它。
int a = readData(); if (a > 0) ...有什么问题?它比试图在一行中塞进太多东西更具可读性。 -
@ajb 限制范围会很有用。
-
@shmosel
{int a = readData(); if (a > 0) ... }
标签: java compiler-errors compilation