/**
* Converts temperature from Celsius to Fahrenheit and Fahrenheit to Celsius
* @Laura
* @1.0
*/
import java.util.*;
public class TempConverter
{
public static void main(String[] args) {
double temp = 0; //initializing variables
int convertChoice = 0;
double tempConvert = 0;
Scanner s = new Scanner(System.in); //creates a scanner
System.out.print("Please enter a temperature: "); //prompts for temperature
temp = s.nextDouble(); //stores user input in temp variable
System.out.print("Enter 1 to convert to Fahrenheit or 2 to convert to Celsius: "); //prompts for choice of Celsius or Fahrenheit
convertChoice = s.nextInt(); //stores user input in convertChoice variable
if (convertChoice == 1) { //if user chooses to convert to Fahrenheit
tempConvert = (1.8 * temp) + 32; //convert using math
System.out.println("The temperature in Fahrenheit is: " + tempConvert); //outputs converted temperature
} else if (convertChoice == 2) { //if user chooses to convert to Celsius
tempConvert = (temp - 32) * 0.5556; //convert using math
System.out.println("The temperature in Celsius is: " + tempConvert); //ouputs converted temperature
//the value may be slightly off due to rounding
} else { //if anything else is entered
System.out.println("That is not a valid choice.");
}
}
}
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.