-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempatureConveter.java
More file actions
66 lines (54 loc) · 2.41 KB
/
TempatureConveter.java
File metadata and controls
66 lines (54 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Scanner;
public class TemperatureConverter {
public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9/5) + 32;
}
public static double celsiusToKelvin(double celsius) {
return celsius + 273.15;
}
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
public static double fahrenheitToKelvin(double fahrenheit) {
return (fahrenheit - 32) * 5/9 + 273.15;
}
public static double kelvinToCelsius(double kelvin) {
if (kelvin < 0) {
System.out.println("Invalid input! Kelvin cannot be negative.");
return -1; // Return an invalid value
}
return kelvin - 273.15;
}
public static double kelvinToFahrenheit(double kelvin) {
if (kelvin < 0) {
System.out.println("Invalid input! Kelvin cannot be negative.");
return -1; // Return an invalid value
}
return (kelvin - 273.15) * 9/5 + 32;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the temperature value: ");
double temperature = scanner.nextDouble();
System.out.print("Enter the scale (Celsius, Fahrenheit, Kelvin): ");
String scale = scanner.next().toLowerCase();
if (scale.equals("celsius")) {
double fahrenheit = celsiusToFahrenheit(temperature);
double kelvin = celsiusToKelvin(temperature);
System.out.println(temperature + " Celsius is " + fahrenheit + " Fahrenheit and " + kelvin + " Kelvin.");
} else if (scale.equals("fahrenheit")) {
double celsius = fahrenheitToCelsius(temperature);
double kelvin = fahrenheitToKelvin(temperature);
System.out.println(temperature + " Fahrenheit is " + celsius + " Celsius and " + kelvin + " Kelvin.");
} else if (scale.equals("kelvin")) {
double celsius = kelvinToCelsius(temperature);
double fahrenheit = kelvinToFahrenheit(temperature);
if (celsius != -1 && fahrenheit != -1) {
System.out.println(temperature + " Kelvin is " + celsius + " Celsius and " + fahrenheit + " Fahrenheit.");
}
} else {
System.out.println("Invalid scale! Please use 'Celsius', 'Fahrenheit', or 'Kelvin'.");
}
scanner.close();
}
}