forked from DSC-Muet-SZAB-Khairpur-Mir-s/Java-Small-Projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Temperature Converter java
98 lines (95 loc) · 3.73 KB
/
Temperature Converter java
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
import java.util.Scanner;
public class Tem_Convert
{
public static void repeat(){
Scanner sc = new Scanner(System.in);
boolean a = true;
awhile(a)
{
char choice;
System.out.println("Do you want continue [y/n]?");
choice = sc.next().charAt(0);
switch(choice){
case 'Y':
Temp_Convert();
break;
case 'y':
Temp_Convert();
break;
case 'n':
a = false;
System.exit(0);
case 'N':
a = false;
System.exit(0);
default:
a = false;
System.exit(0);
}
}
}
public static void Temp_Convert()
{
double celcius, farhenhiet, kelvin;
int option;
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Temperature Converter");
System.out.println("You can Perform any of the following conversions \n");
System.out.println("\tChoose any of the followning Conversion");
System.out.println("1 : Celsius into Farhenhiet");
System.out.println("2 : Farhenhiet into Celsius");
System.out.println("3 : Kelvin into Celsius");
System.out.println("4 : Celsius into Kelvin");
System.out.println("5 : Kelvin into Farhenhiet");
System.out.println("6 : Farhenhiet into Kelvin \n");
System.out.println("Please select an appropriate option(1-6) : ");
option = sc.nextInt();
switch(option)
{
case 1:
System.out.println("Enter temperature in Celcius : ");
celcius = sc.nextDouble();
farhenhiet = (celcius * 9/5)+32;
System.out.println("The temperture in Farhenhiet = "+ String.format("%.2f",farhenhiet)+" F");
break;
case 2:
System.out.println("Enter temperature in Farhenhiet : ");
farhenhiet = sc.nextDouble();
celcius = (5.0/9)*(farhenhiet - 32);
System.out.println("The temperture in Celsius = "+ String.format("%.2f",celcius)+" C");
break;
case 3:
System.out.println("Enter temperature in Kelvin : ");
kelvin = sc.nextDouble();
celcius = kelvin - 273.15;
System.out.println("The temperture in Celsius = "+ String.format("%.2f",celcius)+" C");
break;
case 4:
System.out.println("Enter temperature in Celsuis : ");
celcius = sc.nextDouble();
kelvin = celcius + 273.15;
System.out.println("The temperture in Kelvin = "+ String.format("%.2f",kelvin)+" K");
break;
case 5:
System.out.println("Enter temperature in Kelvin : ");
kelvin = sc.nextDouble();
farhenhiet = ((kelvin-273.15) * 9/5)+32;
System.out.println("The temperture in Farhenhiet = "+ String.format("%.2f",farhenhiet)+" F");
break;
case 6:
System.out.println("Enter temperature in Farhenheit : ");
farhenhiet = sc.nextDouble();
kelvin = ((5.0/9)*(farhenhiet - 32))+273;
System.out.println("The temperture in Kelvin = "+String.format("%.2f",kelvin)+" K");
break;
default:
System.out.println("Invalid entry, Try again");
repeat();
}
}
public static void main (String[] args)
{
Temp_Convert();
repeat();
}
}