Results 1 to 2 of 2
  1. #1
    trill is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Converts from Fahrenheit to Celsius

    Hi, Looking for a little help on writing a temperature conversion program that converts from Fahrenheit to Celsius. I should be able to enter the Fahrenheit temperature from the keyboard.
    Then using a TextField it should display the converted temperature. Of course using the following formula for conversion:

    Java Code:
    Celsius = 5 / 9 * (Fahrenheit - 32)
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class Converter {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String choice;
            do {
                System.out.println("enter fahrenheit");
                int f = Integer.parseInt(scanner.nextLine());
                int celsius = convert(f);
                String s = String.valueOf(f) + " = " + celsius +
                           " celsius";
                JOptionPane.showMessageDialog(null, s, "Converter",
                                     JOptionPane.PLAIN_MESSAGE);
                System.out.println("More? \"y\" or any key");
                choice = scanner.nextLine();
            } while(choice.equals("y"));
            scanner.close();
        }
    
        private static int convert(int fahrenheit) {
            return 5*(fahrenheit - 32)/9;
        }
    }

Similar Threads

  1. Fahrenheit to celsius
    By lalithalydia in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 07:50 AM
  2. farenheit to celsius
    By lalithalydia in forum New To Java
    Replies: 2
    Last Post: 02-02-2008, 08:44 AM
  3. Celsius Converter project
    By jpdamigaman in forum NetBeans
    Replies: 3
    Last Post: 11-14-2007, 12:20 AM
  4. Converts a binary number to a decimal
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •