Why create a Wikiversity account?
Programming Fundamentals/Functions/Java
< Programming Fundamentals​‎ | Functions
functions.java​[​edit source]
// This program asks the user for a Fahrenheit temperature, // converts the given temperature to Celsius,// and displays the results.​//​// References:​// https://www.mathsisfun.com/temperature-conversion.html​// https://en.wikibooks.org/wiki/Java_Programming​import java.util.*​;​class Main { private static Scanner input = new Scanner​(​System​.​in​); public static void main​(​String​[] args) { double fahrenheit​; double celsius; fahrenheit = getFahrenheit​(); celsius = calculateCelsius​(​fahrenheit​); displayResult​(​fahrenheit​, celsius); } private static double getFahrenheit​() { double fahrenheit​; System​.​out​.​println​(​"Enter Fahrenheit temperature:"​); fahrenheit = input​.​nextDouble​(); return fahrenheit​; } private static double calculateCelsius​(​double fahrenheit​) { double celsius; celsius = (​fahrenheit - 32) * 5 / 9; return celsius; } private static void displayResult​(​double fahrenheit​, double celsius) { System​.​out​.​println​(​fahrenheit + "° Fahrenheit is " + celsius + "° Celsius"​); }}
Try It[edit source]
Copy and paste the code above into one of the following free online development environments or use your own Java compiler / interpreter / IDE.
See Also[edit source]
Wikibooks: Java Programming
← Go
Programming Fundamentals/Functions
JavaScript →
Categories: Programming FundamentalsJava
Search
This page was last edited on 19 April 2020, at 14:45.
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.
Privacy policy
About Wikiversity
Disclaimers
Mobile view
Developers
Statistics
Cookie statement
TalkContributionsCreate accountLog in
ResourceDiscuss
ReadEditEdit sourceView history
Visit the main pageMain PageBrowseRecent changesGuided toursRandomHelpDonatePortalColloquiumNewsProjectsSandboxHelp deskWhat links hereRelated changesSpecial pagesPermanent linkPage informationCite this pageCommonsWikibooksWikidataWikinewsWikipediaWikiquoteWikisourceWikispeciesWikivoyageWiktionaryMeta-WikiOutreachMediaWikiWikimaniaCreate a bookDownload as PDFPrintable versionAdd links