Java에서 온도 변환기 만들기
Sheeraz Gul
2023년10월12일
![Java에서 온도 변환기 만들기](/img/Java/feature-image---temperature-converter-java.webp)
온도에는 섭씨, 화씨 및 켈빈의 세 가지 측정 단위가 있습니다. 이 튜토리얼은 모든 유형의 측정 단위에 대해 온도를 한 단위에서 다른 단위로 변환하는 방법을 보여줍니다.
Java에서 온도 변환기 만들기
단위에 따라 한 단위에서 다른 단위로의 변환에는 6가지 유형이 있습니다. 각 변환에는 다른 공식이 있습니다.
아래 표는 온도 변환에 대한 모든 공식을 보여줍니다.
변환 | 공식 |
---|---|
섭씨 받는-사람 화씨 | °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 |
이 6가지 공식을 Java로 구현하여 온도를 변환할 수 있습니다. 우리는 범용 온도 변환기를 만들기 위해 하나의 프로그램에서 6가지 공식을 모두 구현했습니다.
예를 참조하십시오.
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 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