Write a program using loops that allows the user to convert a temperature given
ID: 3642385 • Letter: W
Question
Write a program using loops that allows the user to convert a temperature given in degrees from either Celcius to Farenheit or Fahrenheit to Celcius. Use the following formulas: Degree_C = 5(Degree_f-32)/9 Degree_F= (9(Degree_C/5)+32) Prompt user to enter a temperature and either a C or c for Celcius or an F or f for fahrenheit. Convert the temperature to fahrenheit if celcius is entered, or to celcius if farenheit is entered. Display the result in a readable format. If anything other than C,c,F, or f is entered, print and error message and stop. Do not ask the user to reenter the numeric portion of the temperature again, however. After each conversion ask the user to type Q or q to quit or to press any other key to repeat the loop and perform another conversionExplanation / Answer
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TempConversion {
public static void main(String[] args){
double result;
char degree;
int input;
input = new Integer(JOptionPane.showInputDialog("Enter a temperature: ")).intValue();
degree = JOptionPane.showInputDialog("Celsius or Fahrenheit? ").charAt(0);
if(degree == 'F' || degree == 'f') {
result = 5.0/9.0 * (input - 32);
JOptionPane.showMessageDialog(null, input + " Fahrenheit is " + result + " Celsius");
System.out.println(input + " Fahrenheit is " + result + " Celsius");
}
else if(degree == 'C' || degree == 'c') {
result = 9.0/5.0 * input + 32;
JOptionPane.showMessageDialog(null, input + " Celsius is " + result + " Fahrenheit");
}
else {
System.out.println("Invalid entry");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.