简体   繁体   中英

Error message "Operator '&&' cannot be applied to 'int' 'boolean'

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?

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.

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;
}

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

  }

In your if and else if statements you have the following lines:

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 (i == 0){
    ...
}

The statement inside the if is an boolean. This would do exactly the same, but assigning it to a boolean first:

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM