vendredi 23 décembre 2016

Result from printing statement and if statement differ

I'm a beginner at Java. I came across a question where a teen's age ranges from 13 - 19. There are 2 int values, and I'm supposed to print true if one is a teen, but print false if both are teens.

if ((a >= 13 && a <= 19) && (b >= 13 && b <= 19)) {
  out.println(false);

} 
  out.println(true);

With this code, I was able to generate the right output:

(13, 99) -> expected: true >> run: true

(16, 9) -> expected: true >> run: true

(13, 19) -> expected: false >> run: false

But with this one in particular, I can't: (99, 99) -> expected: false >> run: true.

Now if I try to print it out as a statement:

System.out.println((99 >= 13 && 99 <= 19) && (99 >= 13 && 99 <= 19));

It prints out false. Why is that?

Aucun commentaire:

Enregistrer un commentaire