Write a program the will convert Celsius to Fahrenheit or the other way.The user
ID: 3649582 • Letter: W
Question
Write a program the will convert Celsius to Fahrenheit or the other way.The user is asked to enter a floating point number.The user should be asked to select the conversation that will be performed.The menu should look like the following:C-----From Celsius to Fahrenheit
F-----From Fahrenheit to Celsius
You will need the following function:
Celsius=(5.0/9.0)*(Fahrenheit-32) Fahrenheit=Celsius*(9.0/5.0)+32
Display the results as follows:
You entered Celsius***,the Fahrenheit is***.Or
You entered Fahrenheit***,the Celsius is***.
After the result is displayed the user is prompted to see if another calculation is needed.Have the user enter 'y' or 'Y' to continue or 'n' or 'N' to quit,if the user enter anything else ask his enter again and remind his the input needs to be 'y'('Y') or 'n'('N').
If the user didn't enter C or F for the computation,ask them to enter again.Keep asking the user to enter selection until he enters a valid one.
Explanation / Answer
import java.util.*; //Import to allow you to get user input public class Conversion { public static void main(String[] args) { Scanner in = new Scanner(System.in); //Creates scanner object, requires java.util.Scanner System.out.println("Would you like to convert from Fahrenheit to Celcius or Celcius to Fahrenheit?"); System.out.println("(1) Fahrenheit to Celcius"); System.out.println("(2) Celcius to Fahrenheit"); int choice = in.nextInt(); //Get user input on whether to do F to C or C to F if (choice != 1 && choice != 2) { System.out.println("That was not one of the choices."); //handle if they don't enter a valid choice. } else if (choice == 1) //Fahrenheit to Celcius { System.out.println("Please enter the temperature in Fahrenheit to be converted to Celcius:"); double toConvertFahrenheit = in.nextDouble(); //Get user input for value to convert... Used double to support decimals toConvertFahrenheit = (((toConvertFahrenheit-32)*5)/9); //subtract 32, multiply by 5, then divide by 9 System.out.println("Your converted value is: " + toConvertFahrenheit + " degrees Celcius."); } else if (choice == 2) { System.out.println("Please enter the temperature in Fahrenheit to be converted to Celcius:"); double toConvertCelcius = in.nextDouble(); toConvertCelcius = (((toConvertCelcius*9)/5)+32); //multiply by 9, divide by 5, add 32. System.out.println("Your converted value is: " + toConvertCelcius + " degrees Fahrenheit."); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.