• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to use a variable from one method in another method?

 
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Below is my code. The object of the exercise was to write two functions, one that converts fahrenheit to celsius and another function that prints out both the fahrenheit and celsius temperatures.



The code works - kind of. It returns "F: 95.0
C:0.0" How can I get it to return what was calculated in fahrenheitToCelsius function?

Thanks for your time.
 
Saloon Keeper
Posts: 9565
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, get rid of your static 'celsius' variable.
Then, in place of where you want to print 'celsius' call your fahrenheitToCelsius() method.

You do have a bug in your fahrenheitToCelsius() method though. (5/9) will be performed as an integer divide and will be zero. You want (5.0/9.0) so that the divide takes place as a double precision floating point calculation.
 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And it works! I could have sworn I had written an iteration before where I did place in fahrenheitToCelsius() and it didn't run or returned an error. I didn't think to remove 'static double celsius' though. That might have been it. Thanks!

 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic