Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if statement query

 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the folowwing part of the code. My problem is when i go to the if block,it should calculate fatCalPercent from calories and grams of fat (each gram of ffat is 9 calories),which are given as input in the textfield and display line 1,2 n 3 according to the condition that fatCalPercent <=0.30.
I get the output as 0 in line 1 and line 2 displayed only once and all the other time line 3 is displayed. Please help me out.


 
Daniel Rhoades
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fatCalPercent variable is not declared in your inner class - can we assume it is in the outer class?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I echo Daniel's question, and also point out that I do not see you calculating the fatCalPercent anywhere prior to your if statement. Therefore it is likely being initialized to 0, so your if statement is always true.

You are then calculating the fatCalPercent in the if statement:
    fatCalPercent= ((gramsofFat * 9))/(numberofCalories);
It seems to me that that calculation should occur before your if statement.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real issue is integer math.


Line 1 prints 0 because the result is less than 1 and the result is truncated.

(int * int )/ int

Try using a floating point value for the calories per gram fat conversion:

fatCalPercent= ((gramsofFat * 9.0))/(numberofCalories);
[ March 14, 2005: Message edited by: Carol Enderlin ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic