java 错误消息“运算符 '&&' 不能应用于 'int' 'boolean'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32494677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error message "Operator '&&' cannot be applied to 'int' 'boolean'
提问by user2785079
public class LargestEven {
public int largestEven(int x, int y, int z) {
if(x % 2 = 0 && x > y && x > z) {
return x;
} else if (y % 2 = 0 && y > x && y > z) {
return y;
} else if (z % 2 = 0 && z > x && z > y) {
return z;
} else {
return 0;
}
}
public static void main(String[] args) {
LargestEven l = new LargestEven();
System.out.println(l.largestEven(1, 3, 5)); //prints 0
System.out.println(l.largestEven(2, 4, 9)); //prints 4
System.out.println(l.largestEven(2, 1001, 1003)); //prints 2
}
}
I have to make a program that finds the largest even number out of 3 given numbers. However I can't seem to get it to work because I keep getting this error message. What exactly am I doing wrong here?
我必须编写一个程序,从 3 个给定数字中找出最大的偶数。但是我似乎无法让它工作,因为我不断收到此错误消息。我到底做错了什么?
Sorry for the beginner question, but I've never seen this error message before and have no idea what it means or how to fix it.
很抱歉初学者的问题,但我以前从未见过此错误消息,也不知道它的含义或如何解决它。
Thank you in advance.
先感谢您。
采纳答案by Manikanta Reddy
You have to use == to compare & use = for assignment
你必须使用 == 来比较 & 使用 = 进行赋值
if (x % 2 == 0 && x > y && x > z) {
return x;
} else if (y % 2 == 0 && y > x && y > z) {
return y;
} else if (z % 2 == 0 && z > x && z > y) {
return z;
} else {
return 0;
}
回答by Rustam
You have to check even and odd condition of individual as well as in group for each condition and then check for largest and return.
您必须检查每个条件的个人和组的偶数和奇数条件,然后检查最大和返回。
public int largestEven(int x, int y, int z) {
if (x % 2 == 0 && (y%2!=0 && z%2!=0)) {
return x;
}else if(y%2==0 && (x%2!=0 && z%2!=0) ){
return y;
}else if(z%2==0 && (x%2!=0 && y%2!=0) ){
return z;
}else if(x%2==0 && y%2==0 && z%2!=0){
return x>y?x:y;
}else if(x%2==0 && z%2==0 && y%2!=0){
return x>z?x:z;
}else if(y%2==0 && z%2==0 && x%2!=0){
return y>z?y:z;
}else if(x%2==0 && y%2==0 && z%2==0 ){
return x > y ? (x > z ? x : z) : (y > z ? y : z) ;
}else{
return 0;
}
}
public static void main(String[] args) {
System.out.println(largestEven(6, 3, 4)); //prints 6
System.out.println(largestEven(2, 4, 8)); //prints 8
System.out.println(largestEven(2, 1006, 1003)); //prints 1006
}
回答by Kevin Cruijssen
In your if
and else if
statements you have the following lines:
在您的if
andelse if
语句中,您有以下几行:
x % 2 = 0
Try changing it to this
试试改成这个
x % 2 == 0 // Multiple ==
The single =
is used to assign values, like this:
single=
用于赋值,如下所示:
int i = 0;
And two ==
is used for compares like in your if
and else if
:
两个==
用于比较,例如您的if
and else if
:
if (i == 0){
...
}
The statement inside the if
is an boolean. This would do exactly the same, but assigning it to a boolean
first:
里面的语句if
是一个布尔值。这将完全相同,但将其分配给boolean
第一个:
boolean x = (i == 0);
if (x){ // OR if (x == true){
...
}
I hope the difference is clear now. I also suggest looking a bit more into the basics of Java or programming in general.
我希望现在区别已经很清楚了。我还建议更多地了解 Java 或一般编程的基础知识。
回答by YoungHobbit
You have used assignment operator =
in your conditions instead of ==
equality operator. Please follow the logic below. I have also given a optimized version of it.
您=
在条件中使用了赋值运算符而不是==
相等运算符。请遵循以下逻辑。我也给出了它的优化版本。
When you are looking at x
then make sure other variable (y,z)
are not divisible by 2
and if they do then they are less then x
. Then replicate the same for other conditions.
当您查看时,请x
确保其他变量(y,z)
不是divisible by 2
,如果有,则它们小于x
。然后在其他条件下复制相同的内容。
public int largestEven(int x, int y, int z) {
if(x % 2 == 0 && ((y % 2 != 0) || (y%2 == 0 && y < x)) && ((z % 2 != 0) || (z % 2 == 0 && z < x))) {
return x;
} else if (y % 2 == 0 && ((x % 2 != 0) || (x%2 == 0 && x < y)) && ((z % 2 != 0) || (z % 2 == 0 && z < y))) {
return y;
} else if (z % 2 == 0 && ((x % 2 != 0) || (x%2 == 0 && x < z)) && ((y % 2 != 0) || (y % 2 == 0 && y < z))) {
return z;
} else {
return 0;
}
}
You can further optimize the conditions check using the information you have gained in your previous checks.
您可以使用之前检查中获得的信息进一步优化条件检查。
public int largestEven(int x, int y, int z) {
if(x % 2 == 0 && ((y % 2 != 0) || (y%2 == 0 && y < x)) && ((z % 2 != 0) || (z % 2 == 0 && z < x))) {
return x;
} else if (y % 2 == 0 && ((z % 2 != 0) || (z % 2 == 0 && z < y))) {
return y;
} else if (z % 2 == 0) {
return z;
} else {
return 0;
}
}
Once comparing for x
when we come to y
then we need to worry about x
. because it can not be higher y and divisible by 2 so we can remove that from condition. similarly for z
.
一旦比较x
什么时候我们y
就需要担心了x
。因为它不能大于 y 并且不能被 2 整除,所以我们可以从条件中删除它。对于z
.