在 Java 中建立溫度轉換器
Sheeraz Gul
2022年6月7日
溫度有三種測量單位:攝氏度、華氏度和開爾文。本教程演示瞭如何將所有型別的測量單位的溫度從一種單位轉換為另一種單位。
在 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
Author: Sheeraz Gul
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