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.
提前谢谢你。
4 个解决方案
#1
1
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;
}
#2
1
In your if
and else if
statements you have the following lines:
在if和else if语句中,有以下几行:
x % 2 = 0
Try changing it to this
试着把它改成这个
x % 2 == 0 // Multiple ==
The single =
is used to assign values, like this:
单=用于赋值,如下所示:
int i = 0;
And two ==
is used for compares like in your if
and else if
:
在if和else if中,two ==用于比较:
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 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或编程的基本知识。
#3
1
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)不能被2整除,如果被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。
#4
1
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
}
#1
1
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;
}
#2
1
In your if
and else if
statements you have the following lines:
在if和else if语句中,有以下几行:
x % 2 = 0
Try changing it to this
试着把它改成这个
x % 2 == 0 // Multiple ==
The single =
is used to assign values, like this:
单=用于赋值,如下所示:
int i = 0;
And two ==
is used for compares like in your if
and else if
:
在if和else if中,two ==用于比较:
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 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或编程的基本知识。
#3
1
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)不能被2整除,如果被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。
#4
1
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
}