Here is an example of the return statement in question:

public boolean either24(int[] nums) {
boolean n1 = false;
boolean n2 = false;

  for (int i=0;i<nums.length-1;i++)
  
   if (nums[i]==2 && nums[i+1]==2) 
          n1 = true;
    else
    if (nums[i]==4 && nums[i+1]==4)
    n2 = true;
    
    return !n1 && n2 || n1 && !n2; //<----what is this really doing??
}

What does that line really mean?

Edited 6 Years Ago by jnthn205: n/a

Here is an example of the return statement in question:

public boolean either24(int[] nums) {
boolean n1 = false;
boolean n2 = false;

  for (int i=0;i<nums.length-1;i++)
  
   if (nums[i]==2 && nums[i+1]==2) 
          n1 = true;
    else
    if (nums[i]==4 && nums[i+1]==4)
    n2 = true;
    
    return !n1 && n2 || n1 && !n2; //<----what is this really doing??
}

What does that line really mean?

I haven't tested it yet but wouldn't it mean:

return false and true OR return true and false

I haven't tested it yet but wouldn't it mean:

return false and true OR return true and false

i know that...but how is it determining which to choose?

i know what the !n1 means...but how is the return statement choosing which one?

it is saying return false and true OR true and false.
How does it know which one to choose? Only one boolean type comes out from that, either its true, or its false?

Edited 6 Years Ago by jnthn205: n/a

look at that:

.....
boolean firstAnd =!n1 && n2;
boolean secondAnd=n& && !n2; 
boolean soloOr=firstAnd || secondAnd;
return soloOr;
...
}

look at that:

.....
boolean firstAnd =!n1 && n2;
boolean secondAnd=n& && !n2; 
boolean soloOr=firstAnd || secondAnd;
return soloOr;
...
}

So its returning whichever one is true? Ugh..i dont know why i cant get this.

i just dont get how it can tell if firstAnd || secondAnd; is either true or false. What exactly is it looking for?
if firstAnd is true OR secondAnd is true?<---- this?
if firstAnd is false OR secondAnd is false?<----or this? Or both?

Ok:
If firstAnd egale false AND secondAnd egale false the false result is false
otherwise the result is true.

John,

boolean whatever = first || second;

The above expression will store "true" into the variable "whatever" under the condition that first is true or second is true. If both "first" is false and "second" is false, it will store "false" into "whatever". So, to summarize, unless both "first" and "second" are false, the entire expression evaluates to true, and true gets stored into the variable whatever.

Edited 6 Years Ago by BestJewSinceJC: n/a

Ok:
If firstAnd egale false AND secondAnd egale false the false result is false
otherwise the result is true.

oh ok so if they are both the same value, say both false, then the result is fale? otherwise if it is false/true or true/false, the result is true?

what if they are both true? is the result true?

if they are both the same value, say both false, then the result is false? otherwise if it is false/true or true/false, the result is true?

what if they are both true? is the result true?

Exactly, everything you just said is correct. See my last post in this thread .

I understand now what you mean:
In all cases you function return false guess why??
n1 and n2 are complementary (only n1 or n2 will be true but but not both in the same time; if the two adjacent "cell" in the arry are egale to 2 or 4) in that case you function will return false. because n1 and n2 :
!n1 && n2 is always FALSE
n1 AND!n2 is allways false
so fase OR false is false.

if the to adjacent cases in the array enum are not egale 2 or 4
the result is false because n1 and n2 are initialize to false by defaulte.

It that what you mean?

the Xor function return false if the two values are both true or both false;
The only case where the two values are equal; in our case; is when the two addjacent "cell" are both not egal to 2 or 4;
If it is so we can simply write:

.....
 boolean result =(((nums[i]==2 && nums[i+1]==2)||(nums[i]==4 && nums[i+1]==4))==true)? true: false;
      return result;

this wil gives the same result.

This article has been dead for over six months. Start a new discussion instead.