JAVA不是带有?:if语句

时间:2016-09-18 03:23:50

标签: java

我收到编译错误"不是声明"对于这行代码:

parallel? stats[3]++ : stats[4]++;

无法理解为什么?!

2 个答案:

答案 0 :(得分:4)

:运算符用于返回值不是if / else的完全替换,并且您没有返回值。但是要更好地解释完整错误是什么,并且不仅要发布您发布的行,还要更好地查看代码。

答案 1 :(得分:3)

引自this

The following types of expressions can be made into a statement
by terminating the expression with a semicolon (;).

Assignment expressions
Any use of ++ or --
Method invocations
Object creation expressions

...

In addition to expression statements, there are two other kinds of
statements: declaration statements and control flow statements. 

显然,您上面的代码行不属于上述任何类别。因此,编译器抛出错误。看看最外面的,而不是最里面的。