java - Why does this condition always return false? -
could please tell me wrong following code. doesn't show result.
the integer a,b,c side of right angle triangle.(was solving project euler problem 39)
if use || in place of && , shows desired result based on || condition. doesn't work && condition
public static void main(string[] args) { int a,b,c; (a=1;a<120;a++){ for(b=120;b>0;b--){ c= 120-(a+b); if (((c) > (a+b)) && ((c*c)==(a*a)+(b*b))){ system.out.println(a + " , " + b +" , " + c); system.out.println("**************"); } } } }
quite simply, it's because expression:
((c) > (a+b)) ...never returns true in example valid right angled triangles, , since && condition requires both operands evaluate true, if statement isn't executed.
you can see quite if put in following lines:
system.out.println("c: " + c); system.out.println("a+b: " + a+b);
Comments
Post a Comment