As far as I can see the code is correct.... But i keep getting the output as true instead of false... Why isn't the complement (!) not working?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
As far as I can see the code is correct.... But i keep getting the output as true instead of false... Why isn't the complement (!) not working?
Last edited by helloworld922; February 5th, 2011 at 03:21 PM.
I don't understand what your trying to accomplish with this code. What do you want the code to do?
Last edited by sp11k3t3ht3rd; February 5th, 2011 at 03:11 PM. Reason: I'm confused.
It looks like div is suppose to return if an integer x is divisible by y?
Try stepping through the code (i.e. pretend you're the computer).
1. pig = true or false
4. if (pig == true)?
4a. return pig => return true
5. if (pig == false)?
5a. return !pig => return !false => return true
no matter what pig is, you're always returning true.
Generally you want to create variable names that make sense. x,y, and pig give no indication what that variable is used for. Use a variable name such as numerator, divisor, and isDivisible.
sorry... My bad... I understand now... Thanks everybody...
not sure if this is correct,
But I just though U could replace
return !pig; on line 10 with
return !true;
this gives the false return
IKE
or just use return pig;, no if/else required.
moodycrab3 (February 7th, 2011)