Home > OS >  Four values to be non zero and non negative
Four values to be non zero and non negative

Time:05-15

Suppose I have four variables x, y, z, w. I want to print "hello all non zero values" only if all x,y,z,w are non negative and non zero values. If all the values are zero then print "hello zero". If any of the values(Not all four of them.But it can be either one, two or three of them) is zero or negative then print "illegal values". I wrote a sample solution which handles and clubs negative as well as non negative values below:

 if((x&y&z&w) == 0 && !(x==y && y==z && z==w && w==0)) {
        System.out.println("illegal values");
    } else {
        System.out.println("hello all non zero values");
    }

I am not able to think to handle the negative and positive values separately. Can anyone please suggest a solution for it?

CodePudding user response:

It might help to reword your question in a different but equivalent way. You are basically printing "hello zero" if all of them are zero, "hello all non zero values" if all of them are positive, and "illegal values" in all other cases.

if (x == 0 && y == 0 && z == 0 && w == 0) {
    System.out.println("hello zero");
} else if (x > 0 && y > 0 && z > 0 && w > 0) {
    System.out.println("hello all non zero values");
} else {
    System.out.println("illegal values");
}

CodePudding user response:

Does this work for you?

if  ((x>0)&(y>0)&(z>0)&(w>0)) {
    System.out.println("hello all non zero values");
} else {
    System.out.println("illegal values");
}

CodePudding user response:

Something like this?

if (x > 0 && y > 0 && z > 0 && w > 0) {
   System.out.println("hello all non zero values");
} else if (x == 0 && y == 0 && z == 0 && w == 0) {
   System.out.println("hello zero");
} else {
   System.out.println("illegal values");
}
  • Related