在if语句中分配变量值 [英] Assign variable value inside if-statement
本文介绍了在if语句中分配变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以在条件运算符中为变量赋值,如下所示:
I was wondering whether it is possible to assign a variable a value inside a conditional operator like so:
if((int v = someMethod())!= 0)return v;
有没有办法在Java中做到这一点?因为我知道在的情况下可以使用
条件,但是我不确定我是否对if语句做错了,或者它是不可能的。
Is there some way to do this in Java? Because I know it's possible in while
conditions, but I'm not sure if I'm doing it wrong for the if-statement or if it's just not possible.
推荐答案
变量可以在条件语句中分配但不能声明:
Variables can be assigned but not declared inside the conditional statement:
int v;
if((v = someMethod()) != 0) return true;
这篇关于在if语句中分配变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文