三元运算符在 Android 中不起作用

发布于 2024-11-16 10:17:53 字数 332 浏览 6 评论 0原文

我有一个简单的问题让我困惑。我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-11-23 10:17:53

这与安卓无关。您不能将条件表达式单独用作语句...并且第二个和第三个操作数也不能是 void 表达式。

您应该使用:

lendBtn.setChecked(amt < 0);

... 这更容易开始。

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:

lendBtn.setChecked(amt < 0);

... which is simpler to start with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文