TempConverter

/** * Converts temperature from Celsius to Fahrenheit and Fahrenheit to Celsius * * @Laura * @1.1 */ import java.util.*; public class TempConverter { public static void main(String[] args) { convert(); Scanner continueConvert = new Scanner(System.in); System.out.print("Would you like to continue? Enter \"yes\" or \"no\": "); String input = continueConvert.nextLine(); while ((input.toLowerCase()).equals("yes")) { convert(); System.out.print("Would you like to continue? Enter \"yes\" or \"no\": "); input = continueConvert.nextLine(); } System.out.println("Program terminated."); } public static void convert() { 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 = ((9.0/5) * 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) * (5/9.0); //convert using math System.out.println("The temperature in Celsius is: " + tempConvert); //ouputs converted temperature } 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.