简体   繁体   English

运算符'&&'不能应用于'bool'和'int'类型的操作数

[英]Operator '&&' cannot be applied to operand of type 'bool' and 'int'

I have an if elseif statement to check marks and grade the marks according to the condition. 我有一个if elseif语句来检查标记并根据条件对标记进行评分。

int marks;
string grade;

if (marks>=80 && marks!>100)
{
  grade = "A1";
}
else if (marks>=70 && marks!>79)
{
  grade = "A2";
}

and so on..... 等等.....

however, when i compile it i got 但是,当我编译它时,我得到了

Operator '&&' cannot be applied to operand of type 'bool' and 'int' 运算符'&&'不能应用于'bool'和'int'类型的操作数

help me fix it.thanks in advance. 帮我修好它。谢谢你提前。

That is not a real operator: 那不是一个真正的运营商:

!>

Not Greater Than would be <= (Less Than or Equal To) 不大于<= (小于或等于)

EDIT: What you are trying to say could actually also be expressed using the ! 编辑:你想说的话实际上也可以用!来表达! operator. 运营商。 But it would be 但它会

!(marks > 100 )

you have used the wrong operator, 你使用了错误的操作员,

it must be. 一定是。

int marks;
string grade;

if (marks>=80 && marks<=100)
{
  grade = "A1";
}
elseif (marks>=70 && marks<=79)
{
  grade = "A2";
}

Also you can do is 你也可以这样做

int marks;
string grade;

if (marks>=80 && !(marks>100))
{
  grade = "A1";
}
elseif (marks>=70 && !(marks>79))
{
  grade = "A2";
}

Other answers have made it known that the main problem is that !> isn't an operator. 其他答案让人们知道主要问题是!>不是运营商。

I'd like to suggest that since you're testing whether marks lies within particular ranges that you take an additional further step to format your conditional expressions to use the following pattern: 我想建议,既然您正在测试marks是否位于特定范围内,那么您需要采取进一步的步骤来格式化条件表达式以使用以下模式:

if (80 <= marks && marks <= 100)
{
  grade = "A1";
}
else if (70 <= marks && marks <= 79)
{
  grade = "A2";
}

It's a simple and maybe subtle change, but I think it makes the range check intent much more clear. 这是一个简单而且微妙的变化,但我认为这使得范围检查意图更加清晰。

int marks;
string grade;

if ((marks>=80) && !(marks > 100))
{
grade = "A1";
}
else if ((marks>=70) && !(marks > 79))
{
grade = "A2";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 操作员! 不能应用于Task类型的操作数 <bool> - Operator ! cannot be applied to operand of type Task<bool> 运算符“ ”不能应用于类型的操作数<int>和空 - Operator ' ' cannot be applied to operand of type <int> and null 错误:操作员&#39;!&#39; 不能应用于&#39;int&#39;类型的操作数 - Error: Operator '!' cannot be applied to operand of type 'int' 运算符&#39;!=&#39;不能应用于&#39;bool&#39;类型的操作数? 和&#39;int&#39; - Operator '!=' cannot be applied to operands of type 'bool?' and 'int' 运算符 &amp;&amp; 不能应用于“int”和“bool”类型的操作数 - Operator && cannot be applied to operands of type 'int' and 'bool' 运算符“ ||”不能应用于类型为“ bool”和“ int”的操作数 - Operator “||” cannot be applied to operands of type 'bool' and 'int' 运算符“ &lt;&lt;”不能应用于类型为“ bool”和“ int”的操作数 - operator '<<' cannot be applied to operands of type 'bool' and 'int' 运算符|| 不能应用于int和bool - operator || cannot be applied to int and bool 运算符“ &amp;&amp;”不能应用于类型为“ int”和“ bool” linq的操作数 - operator '&&' cannot be applied to operands of type 'int' and 'bool' linq 使用按位和运算符时,不能将运算符&应用于int和bool类型的操作数 - operator & cannot be applied to operands of type int and bool when using bitwise and
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM