This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTemperatureConverter.java
More file actions
99 lines (88 loc) · 3.47 KB
/
TemperatureConverter.java
File metadata and controls
99 lines (88 loc) · 3.47 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Temperature Converter");
System.out.println("----------------------");
System.out.println("Select the input temperature scale:");
System.out.println("1. Celsius");
System.out.println("2. Fahrenheit");
System.out.println("3. Kelvin");
System.out.print("Enter your choice (1-3): ");
int inputChoice = scanner.nextInt();
System.out.print("Enter the temperature to convert: ");
double inputTemperature = scanner.nextDouble();
System.out.println("Select the output temperature scale:");
System.out.println("1. Celsius");
System.out.println("2. Fahrenheit");
System.out.println("3. Kelvin");
System.out.print("Enter your choice (1-3): ");
int outputChoice = scanner.nextInt();
double convertedTemperature = 0;
switch (inputChoice) {
case 1: // Celsius
convertedTemperature = convertFromCelsius(inputTemperature, outputChoice);
break;
case 2: // Fahrenheit
convertedTemperature = convertFromFahrenheit(inputTemperature, outputChoice);
break;
case 3: // Kelvin
convertedTemperature = convertFromKelvin(inputTemperature, outputChoice);
break;
default:
System.out.println("Invalid input scale choice.");
return;
}
String inputScale = getScaleName(inputChoice);
String outputScale = getScaleName(outputChoice);
System.out.printf("The temperature %.2f %s is equal to %.2f %s.\n", inputTemperature, inputScale, convertedTemperature, outputScale);
}
private static double convertFromCelsius(double temp, int outputChoice) {
switch (outputChoice) {
case 1: // Celsius
return temp;
case 2: // Fahrenheit
return (temp * 9/5) + 32;
case 3: // Kelvin
return temp + 273.15;
default:
throw new IllegalArgumentException("Invalid output scale choice.");
}
}
private static double convertFromFahrenheit(double temp, int outputChoice) {
switch (outputChoice) {
case 1: // Celsius
return (temp - 32) * 5/9;
case 2: // Fahrenheit
return temp;
case 3: // Kelvin
return (temp - 32) * 5/9 + 273.15;
default:
throw new IllegalArgumentException("Invalid output scale choice.");
}
}
private static double convertFromKelvin(double temp, int outputChoice) {
switch (outputChoice) {
case 1: // Celsius
return temp - 273.15;
case 2: // Fahrenheit
return (temp - 273.15) * 9/5 + 32;
case 3: // Kelvin
return temp;
default:
throw new IllegalArgumentException("Invalid output scale choice.");
}
}
private static String getScaleName(int choice) {
switch (choice) {
case 1:
return "Celsius";
case 2:
return "Fahrenheit";
case 3:
return "Kelvin";
default:
return "";
}
}
}