• Post Reply Bookmark Topic Watch Topic
  • New Topic

switch variables  RSS feed

 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written this problem but the result is always zero, I don't know why despite that the logic is revised.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 25.
5/9 = 0 (integer division)

The same problem in line 32.

A tip: Both fahrenheitToCelsius and celsiusToFahrenheit could be written in one line instead of three lines.

Like:
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, I changed this division and it works. I thought that as the variable was declared double so, the portion of 5/9 will also returns double or it will be promoted to.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:thank you, I changed this division and it works. I thought that as the variable was declared double so, the portion of 5/9 will also returns double or it will be promoted to.

Yes, 5/9 will be promoted to double but after the calculation. The calculation of 5/9 gives 0, so it is promoted to 0.0.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:I thought that as the variable was declared double so, the portion of 5/9 will also returns double or it will be promoted to.

Afraid not. The value is first calculated and then this value is converted to double format during the assignment, so 5/9 is 0 and your variable will contain 0.0.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!