• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

if statement query

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fatCalPercent variable is not declared in your inner class - can we assume it is in the outer class?
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic