三元运算符在Java中有多个条件引发错误 [英] Ternary operator with multiple condtions in Java throwing error

查看:161
本文介绍了三元运算符在Java中有多个条件引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Java中的三元条件。

This is my ternary condition in Java.

new_user_id.equals(userid) && new_key.equals(key) && !new_value.equals(value) ? updateValue() : System.out.println("New value already exists in DB");

我正在尝试匹配三个条件,如果所有三个条件都成立,则调用updateValue()函数。但是仍然会出现抛出错误分配的左侧必须是变量。有想法吗?

I'm trying to match three conditions and call updateValue() function if all three conditions gets true. But still its throwing error "The left-hand side of an assignment must be a variable". Any ideas?

推荐答案

三元运算符用于条件变量赋值或类似的操作:

The ternary operator is used for conditional variable assignment or something like:

System.out.println("You have " + items + (items == 1 ? "item." : "items."));

因此,它必须返回某些内容。请使用 if 语句。

Therefore, it must return something. Use an if statement instead.

这篇关于三元运算符在Java中有多个条件引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆