TemperatureConverter

/* Your task is to create a program that asks the user (scanner class) to enter a temperature. They should then be prompted to enter 1 to convert from Celsius to Fahrenheit and 2 to convert from Fahrenheit to Celsius. Formula is provided below. Converting Celsius To Fahrenheit The equation for converting temperature from Celsius to Fahrenheit is (9/5 * C) + 32 = F. Converting Fahrenheit To Celsius The equation for converting temperature from Fahrenheit to Celsius is (F – 32) * 5/9 = C. /** * This class will allow you to convert any temperature from Celsius to Fahrenheit, or from Fahrenheit to Celsius. * * @author Sameer Saxena * @version 1.0 */ import java.util.Scanner; public class TemperatureConverter { public static void main(String args[]){ while (true) { //Creating first Scanner for input of temperature value Scanner scan = new Scanner(System.in); System.out.print("Please enter a temperature: "); int nextInt = scan.nextInt(); //Creating second Scanner for input to determine whether original temperature is in Celsius or Fahrenheit System.out.print("Type 1 if the temperature above is in Celsius, type 2 if it is in Fahrenheit: "); int next = scan.nextInt(); if (next == 1) { //If temperature is in Celsius, use this formula to convert temperature into Fahrenheit double fahrenheit = (9.0/5 * nextInt) + 32; System.out.println("The result is " + fahrenheit + " degrees Fahrenheit"); System.out.println(""); } else { //If temperature is in Fahrenheit, use this formula to convert temperature into Celsius double celsius = (nextInt - 32) * (5.0/9); System.out.println("The result is " + celsius + " degrees Celsius"); System.out.println(""); } //Asking user if they would like to repeat program System.out.print("Would you like to convert another temperature? Type 1 if this is the case, and 0 if you would like to close the program: "); int another = scan.nextInt(); if (another != 1) { //If the input was not equal to "1", stop program break; } System.out.println(""); } } //End of main method } //End of class TemperatureConverter
Converts temperature from Fahrenheit to Celsius, or the other way around.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.