我正在尝试将2个变量与枚举类型进行比较

时间:2019-10-28 12:56:58

标签: java android-studio enums operators

我创建了一个Enum磨损状态,然后将wearState作为两个变量的类型。但是当我尝试使用'||'将它们置于if条件时或“ &&”,它不起作用。 它说:“错误:二进制运算符'||'的错误操作数类型“

enum wearState {
        WRIST_MOVE,
        WRIST_IMMOBILE,
        BELT_NECK_MOVE,
        BELT_NECK_IMMOBILE,
        OTHER_IMMOBILE_STATE;
    }


enum chargingState {
        YES,
        NO,
    }

wearState lastState;
wearState currentState;
chargingState lastStateCharging;
chargingState currentStateCharging;

if (((currentState = wearState.BELT_NECK_IMMOBILE) || (currentState = wearState.WRIST_IMMOBILE)) && 
    (lastStateCharging = chargingState.NO)) {

              /* .... */
 }

1 个答案:

答案 0 :(得分:0)

if (((currentState == wearState.BELT_NECK_IMMOBILE) || (currentState == wearState.WRIST_IMMOBILE)) && 
(lastStateCharging == chargingState.NO))

在比较两个变量是否请使用“ ==”时, =是赋值运算符。 它只能用于为变量赋值,例如

String name="Joe";

要与变量进行比较,我们应该使用比较运算符。 ==,<=,> =,!=,<,> 是比较运算符;