编辑:对不起,我已经意识到Scala肯定会像我问的那样做同样的事情.请继续尝试.在提出问题之前,我会确保我尝试了这些代码.感谢帮助.
如果我想这样做:
if (!(condition)) { }
Run Code Online (Sandbox Code Playgroud)
Scala中的等效表达式是什么?看起来像吗?
if (not(condition)) { }
Run Code Online (Sandbox Code Playgroud)
例如,在C++中,我可以这样做:
bool condition = (x > 0)
if(!condition){ printf("x is not larger than zero") }
Run Code Online (Sandbox Code Playgroud)
jrb*_*ard 12
在Scala中,您可以检查if
两个操作数是否为equal(==
)或不是(!=
),如果满足条件则返回true,否则返回false(else
).
if(x!=0) {
// if x is not equal to 0
} else {
// if x is equal to 0
}
Run Code Online (Sandbox Code Playgroud)
它本身!
被称为逻辑非运算符.用它来反转其操作数的逻辑状态.如果条件为真,则Logical NOT运算符将使其为false.
if(!(condition)) {
// condition not met
} else {
// condition met
}
Run Code Online (Sandbox Code Playgroud)
哪里condition
可以是任何逻辑表达式.即:x==0
使其行为与上面的第一个代码示例完全相同.