• Post Reply Bookmark Topic Watch Topic
  • New Topic

If and Else with <1.0  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My below code. I am having trouble with the setLength. It's supposed to send the value of 0.0 IF the length value is less than 0.0f and 51.0f if it's greater than 50.0f otherwise ( else ) just send whatever the value is. Well when the value is greater than 50.0f it works as expected but when the value is less than 1.0f ( say .10f or .5f) it does not. It's sending 0.0 but instead it sends whatever the value is.

I asked my instructor about this but I don't understand his response to me. He says "If length is -4.0 you'll set lengthVerified to 0.0 but then set it back to -4.0 since -4.0 > 50.0 is false." What does this mean? I'm not using -4.0 and how is -4.0 greater than 50.0? Thank You

 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For one thing you are using doubles and "==", doubles are an approximation and not an exact number. So in your example you are using comparisons of > and < which work fine, if you have a value of myValue < 0, that will happen in a double or floatingpoint math, where myValue == 0 is not as probable and in your myValue > 50 is also very probable where myValue == 50 is far less probable.

So look at your comparisons, they don't make any sense--in your main.

Also in your setLength, you are missing an else on the first if clause.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!