-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureConverter.java
More file actions
82 lines (72 loc) · 2.58 KB
/
TemperatureConverter.java
File metadata and controls
82 lines (72 loc) · 2.58 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
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.print("Enter temperature value: ");
if (!scanner.hasNextDouble()) {
System.out.println("Invalid temperature.");
scanner.close();
return;
}
double value = scanner.nextDouble();
System.out.print("Convert to (C/F): ");
String unit = scanner.next().trim().toUpperCase();
if (unit.equals("C")) {
double celsius = (value - 32) * 5.0 / 9.0;
System.out.printf("%.2f F = %.2f C%n", value, celsius);
} else if (unit.equals("F")) {
double fahrenheit = value * 9.0 / 5.0 + 32;
System.out.printf("%.2f C = %.2f F%n", value, fahrenheit);
} else {
System.out.println("Unknown unit. Please use 'C' or 'F'.");
}
scanner.close();
=======
private static double cToF(double c) {
return c * 9.0 / 5.0 + 32.0;
}
private static double fToC(double f) {
return (f - 32.0) * 5.0 / 9.0;
}
private static double cToK(double c) {
return c + 273.15;
}
private static double kToC(double k) {
return k - 273.15;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("=== Temperature Converter ===");
System.out.println("1) Celsius -> Fahrenheit");
System.out.println("2) Fahrenheit -> Celsius");
System.out.println("3) Celsius -> Kelvin");
System.out.println("4) Kelvin -> Celsius");
System.out.print("Choose option (1-4): ");
int choice = sc.hasNextInt() ? sc.nextInt() : -1;
if (choice < 1 || choice > 4) {
System.out.println("Invalid option.");
return;
}
System.out.print("Enter value: ");
if (!sc.hasNextDouble()) {
System.out.println("Invalid number.");
return;
}
double val = sc.nextDouble();
switch (choice) {
case 1:
System.out.printf("Result: %.2f °F%n", cToF(val));
break;
case 2:
System.out.printf("Result: %.2f °C%n", fToC(val));
break;
case 3:
System.out.printf("Result: %.2f K%n", cToK(val));
break;
case 4:
System.out.printf("Result: %.2f °C%n", kToC(val));
break;
}
}
}