Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

How to convert between Fahrenheit, Celsius, and Kelvin in Java

1 Answer

0 votes
public class Main {

    public static void temperatureConvert(double temperature, char unitType, double[] results) {
        switch (unitType) {
            case 'c':
                results[0] = ((9.0 / 5.0) * temperature) + 32; // Fahrenheit
                results[1] = (temperature + 273.15); // Kelvin
                break;
            case 'f':
                results[0] = (temperature - 32) / 1.8; // Celsius
                results[1] = (temperature - 32) * 5 / 9.0 + 273.15; // Kelvin
                break;
            case 'k':
                results[0] = (temperature - 273.15); // Celsius
                results[1] = (((9.0 / 5.0) * temperature) - 459.67); // Fahrenheit
                break;
            default:
                results[0] = results[1] = 0;
        }
    }

    public static void main(String[] args) {
        double[] result = new double[2];

        temperatureConvert(24, 'c', result);
        System.out.println("Fahrenheit is: " + result[0] + " degrees\n" +
                "Kelvin     is: " + result[1] + " degrees\n");

        temperatureConvert(3, 'f', result);
        System.out.println("Celsius is: " + result[0] + " degrees\n" +
                "Kelvin  is: " + result[1] + " degrees\n");

        temperatureConvert(3, 'k', result);
        System.out.println("Celsius    is: " + result[0] + " degrees\n" +
                "Fahrenheit is: " + result[1] + " degrees\n");
    }
}




/*
run:
    
Fahrenheit is: 75.2 degrees
Kelvin     is: 297.15 degrees

Celsius is: -16.11111111111111 degrees
Kelvin  is: 257.0388888888889 degrees

Celsius    is: -270.15 degrees
Fahrenheit is: -454.27000000000004 degrees

*/

 



answered Dec 15, 2024 by avibootz

Related questions

1 answer 86 views
1 answer 107 views
1 answer 97 views
1 answer 87 views
1 answer 91 views
1 answer 84 views
1 answer 83 views
...