运营商&&'不能应用于'int'和'bool'类型的操作数 [英] Operator '&&' can't be applied to operands of type 'int' and 'bool'

查看:160
本文介绍了运营商&&'不能应用于'int'和'bool'类型的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出输入的数字是否可以被9和13整除,但它不会让我使用我需要的运算符,我知道变量类型是错误的但我不知道如何使它如此变量类型被接受,我是编码的新手,所以答案尽可能基本没有小便

im trying to find out if a number that is entered is both divisible by 9 and 13, but it wont let me use the operator i need, i know the variable types are wrong but i dont know how to make it so that the variable types are accepted, im new to coding so can the answer be as basic as possible without taking the piss

public bool IsFizzBuzz(int input)
{
    if ((input % 9) && (input % 13) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}


推荐答案

== operator 优先级高于 && operator ,你的if语句先计算;

Since == operator has higher precedence than && operator, your if statements calculates first;

(input % 13) == 0

part并返回 true false 取决于您的输入。你的if语句就像;

part and that returns true or false depends on your input. And your if statement will be like;

(input % 9) && true // or false

,因为输入%9 表达式返回 int ,最后,你的if语句将是;

since input % 9 expression returns int, at the end, your if statement will be;

int && true

逻辑AND在 int 之间没有意义和 bool 变量。

and logical AND is meaningless between int and bool variables.

来自 && 运营商(C#参考)

From && Operator (C# Reference)


条件AND运算符(&&)执行其 bool
操作数
的逻辑AND,但仅计算其第二个操作数如有必要。

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.

你说;


我试图找出输入的数字是否可以被
9和13整除

im trying to find out if a number that is entered is both divisible by 9 and 13

然后你应该使用他们喜欢;

Then you should use them like;

if ((input % 9 == 0) && (input % 13 == 0))

这篇关于运营商&&'不能应用于'int'和'bool'类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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