Exercise 5 : Precendence


#1



public class Precedence {
	public static void main(String[] args) {

		boolean riddle = true;
      riddle =( (1 < 8) || (5 > 2 || 3 < 5));
		System.out.println(riddle);
    

	}
}

i don't understand this exercise. My code statement print "false" but i can't pass this exercise


#2

hi
boolean riddle =!(1>8)&&(5<2 || 3<5));
if it errors click (Get help { right and down }, i want to restart this exercise) then write upper code again:)


#3

hi
boolean riddle = !( 1 < 8 || (5 > 2 && 3 < 5));
try dis out.....


#4

"Use each Boolean operator no more than once "

You only used the OR operator.

If you are unsure why dsupraja's answer is correct, keep in mind that parenthesis influence the order of things.

Precedence
1. ( )
2. !
3. &&
4. ||

So you have to work your way from the inner () out. Also if you assume the left most operator will be the "!" then the result of the inner () must evaluate to true. Otherwise the ! can't flip it to false.

!(true) == false
!(false) == true

BTW, if any reading this gets the "invalid start of type" look at you comparisons. It took me a minute to see I accidentally deleted the left number in the last comparison.

boolean riddle = !( 1 < 8 && (5 > 2 && < 5));


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.