You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复‘opperator && 无法应用于boolean’错误?尝试(requestCode)&&(requestCode=Result)无效

Fix for "operator && cannot be applied to boolean [non-boolean type]" Error

Hey there, let's break down what's going on here and fix that error.

First, let's clarify why you're seeing this message: Most strongly-typed languages (like Java, C#, etc.) don't let you use non-boolean values directly with the && logical operator. The && requires both sides of the operator to evaluate to a boolean (true/false), but your original code (and your attempted fix) is using a value like an integer (requestCode) directly, which isn't a boolean.

Your attempted fix (requestCode)&&(requestCode=Result) doesn't work for two key reasons:

  • The left side requestCode is still a non-boolean value (like an int), so the language can't interpret it as a boolean for the && check.
  • The right side requestCode=Result is an assignment, not a boolean check. This returns the value being assigned (the value of Result), which is also non-boolean—so even if the left side worked, the right side would still trigger the same error.

Here's how to fix it, depending on your goal:

Scenario 1: You want to check if requestCode is valid (e.g., non-zero) AND equals Result

If your aim is to run code only when requestCode meets both conditions, rewrite the expression to use explicit boolean checks on both sides:

// Example for int-type requestCode
if (requestCode != 0 && requestCode == Result) {
    // Your code here
}
  • requestCode != 0 converts the numeric value to a boolean (true if non-zero, false otherwise)
  • requestCode == Result checks if the values match, also returning a boolean
  • Now && has two valid boolean operands, which is exactly what the language expects.

Scenario 2: You want to assign Result to requestCode only if requestCode is valid

If you're trying to conditionally assign the value (not just check it), you can't use && here. Instead, use an if statement:

if (requestCode != 0) { // Adjust the condition to match your "valid" definition
    requestCode = Result;
}

Or if you prefer a shorter one-liner, use the ternary operator:

requestCode = (requestCode != 0) ? Result : requestCode;

Quick recap

Always make sure both sides of && evaluate to a boolean. Never use raw numeric/non-boolean values directly with logical operators in strongly-typed languages—explicitly write the boolean check (like != 0, == someValue, etc.).

内容的提问来源于stack exchange,提问作者Kadi Simpson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:00:26