三元运算符在 Android 中不起作用
我有一个简单的问题让我困惑。我正在尝试在java中使用三元运算符。我是 Android 和 java 新手。这段代码给了我错误:
amt < 0 ? lendBtn.setChecked(true) : lendBtn.setChecked(false);
"Syntax error on token "<", invalid AssignmentOperator"
所以,我用 if 语句替换它,它完全有效:
if (amt < 0) { ... }
这不是什么大问题,但有人知道为什么吗?
I have a simple question that boggles me. I am trying to use the ternary operator in java. I am new to Android and java. This code gives me the error:
amt < 0 ? lendBtn.setChecked(true) : lendBtn.setChecked(false);
"Syntax error on token "<", invalid AssignmentOperator"
So, I replace it with an if statement and it totally works:
if (amt < 0) { ... }
It's not a big deal but does anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与安卓无关。您不能将条件表达式单独用作语句...并且第二个和第三个操作数也不能是 void 表达式。
您应该使用:
... 这更容易开始。
This has nothing to do with Android. You can't use a conditional expression as a statement on its own... and the second and third operands can't be void expressions either.
You should use:
... which is simpler to start with.