Fahrenheit To Celsius Example.
Step 1 : I use JOptionPane.showInputDialog to get the fahrenheit reading
Step 2 : calculate celsius using the following calculate
double celsius = (5.0/9)*(fahrenheit- 32);
Step 3 : Show the result using JOptionPane.showMessageDialog
Code below :
// Celsius to Fahrenheit Converter Example
import javax.swing.JOptionPane;
public class Fahrenheit{
public static void main(String[] args){
String number = JOptionPane.showInputDialog(null, "Enter fahrenheit value : " , "Fahrenheit to Celsius Converter", JOptionPane.QUESTION_MESSAGE);
//convert string to number
double fahrenheit = Double.parseDouble(number);
double celsius = (5.0/9)*(fahrenheit- 32);
JOptionPane.showMessageDialog(null, "Fahrenheit value is " + fahrenheit + "\n" + "Celcius value is " + celsius ,
"Fahrenheit to Celsius", JOptionPane.INFORMATION_MESSAGE);
}
}
No comments:
Post a Comment