在 Java 中创建温度转换器

Sheeraz Gul 2023年10月12日
在 Java 中创建温度转换器

温度有三种测量单位:摄氏度、华氏度和开尔文。本教程演示了如何将所有类型的测量单位的温度从一种单位转换为另一种单位。

在 Java 中创建温度转换器

根据单位,从一个单位到另一种单位的转换可以有六种类型。每次转换都有不同的公式。

下表演示了温度转换的所有公式:

转换 公式
摄氏度到华氏度 °F = (°C × 9/5) + 32
摄氏度到开尔文 °K = °C + 273.15
华氏度到摄氏度 °C = (°F − 32) × 5/9
华氏度到开尔文 °K = (°F − 32) × 5/9 + 273.15
开尔文到摄氏度 °C = °K − 273.15
开尔文到华氏度 °F = (°K − 273.15) × 9/5 + 32

这六个公式可以用 Java 实现来转换温度。我们在一个程序中实现了所有六个公式,以创建通用温度转换器。

参见示例:

package delftstack;

import java.util.*;
public class Temprature_Converter {
  static Scanner sc = new Scanner(System.in); // Scanner Class

  // Celcius to Fahrenheit
  static double Celcius_to_Fahrenheit(double Celcius) {
    double Fahrenheit = (Celcius * 9 / 5) + 32;
    return Fahrenheit;
  }
  // Celcius to Kelvin
  static double Celcius_to_Kelvin(double Celcius) {
    double Kelvin = Celcius + 273.15;
    return Kelvin;
  }

  // Fahrenheit to Celcius
  static double Fahrenheit_to_Celcius(double Fahrenheit) {
    double Celcius = (Fahrenheit - 32) * 5 / 9;
    return Celcius;
  }
  // Fahrenheit to Kelvin
  static double Fahrenheit_to_Kelvin(double Fahrenheit) {
    double Kelvin = (Fahrenheit - 32) * 5 / 9 + 273.15;
    return Kelvin;
  }

  // Kelvin to Celcius
  static double Kelvin_to_Celcius(double Kelvin) {
    double Celcius = Kelvin - 273.15;
    return Celcius;
  }
  // Kelvin to Fahrenheit
  static double Kelvin_to_Fahrenheit(double Kelvin) {
    double Fahrenheit = (Kelvin - 273.15) * 9 / 5 + 32;
    return Fahrenheit;
  }

  // Read the value of temperature given by the user
  static double Input_Temperature(String Temperature) {
    System.out.println("Enter the " + Temperature + " value:");
    double val = sc.nextDouble();
    return val;
  }

  // Print converted value of temperature
  static void Output_Temperature(double Value, String Temperature) {
    System.out.println("The " + Temperature + " value is: " + Value);
  }

  // Driver Method
  public static void main(String args[]) {
    System.out.println("Enter 1 for Celcius to Fahrenheit\nEnter 2 for Celcius to Kelvin\n"
        + "Enter 3 for Fahrenheit to Celcius\nEnter 4 for Fahrenheit to Kelvin\n"
        + "Enter 5 for Kelvin to Celcius\nEnter 6 for Kelvin to Fahrenheit\n7. Exit");
    do {
      System.out.println("\nEnter Your Temperature Choice Number: ");
      int Temprature_Choice = sc.nextInt();
      double Choice_Number = 0;
      switch (Temprature_Choice) {
        case 1:
          Choice_Number = Input_Temperature("Celcius");
          Output_Temperature(Celcius_to_Fahrenheit(Choice_Number), "Fahrenheit");
          break;
        case 2:
          Choice_Number = Input_Temperature("Celcius");
          Output_Temperature(Celcius_to_Kelvin(Choice_Number), "Kelvin");
          break;
        case 3:
          Choice_Number = Input_Temperature("Fahrenheit");
          Output_Temperature(Fahrenheit_to_Celcius(Choice_Number), "Celcius");
          break;
        case 4:
          Choice_Number = Input_Temperature("Fahrenheit");
          Output_Temperature(Fahrenheit_to_Kelvin(Choice_Number), "Kelvin");
          break;
        case 5:
          Choice_Number = Input_Temperature("Kelvin");
          Output_Temperature(Kelvin_to_Celcius(Choice_Number), "Celcius");
          break;
        case 6:
          Choice_Number = Input_Temperature("Kelvin");
          Output_Temperature(Kelvin_to_Fahrenheit(Choice_Number), "Fahrenheit");
          break;
        case 7:
          System.exit(0);
          break;
        default:
          System.out.println("Invalid Input");
      }
    } while (true);
  }
}

上面的代码将用户输入作为选择编号,然后是要转换的温度,然后根据选择进行转换。

见输出:

Enter 1 for Celcius to Fahrenheit
Enter 2 for Celcius to Kelvin
Enter 3 for Fahrenheit to Celcius
Enter 4 for Fahrenheit to Kelvin
Enter 5 for Kelvin to Celcius
Enter 6 for Kelvin to Fahrenheit
7. Exit

Enter Your Temperature Choice Number:
1
Enter the Celcius value:
50
The Fahrenheit value is: 122.0

Enter Your Temperature Choice Number:
2
Enter the Celcius value:
50
The Kelvin value is: 323.15

Enter Your Temperature Choice Number:
3
Enter the Fahrenheit value:
50
The Celcius value is: 10.0

Enter Your Temperature Choice Number:
4
Enter the Fahrenheit value:
50
The Kelvin value is: 283.15

Enter Your Temperature Choice Number:
5
Enter the Kelvin value:
50
The Celcius value is: -223.14999999999998

Enter Your Temperature Choice Number:
6
Enter the Kelvin value:
50
The Fahrenheit value is: -369.66999999999996

Enter Your Temperature Choice Number:
7
作者: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook