forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.java
More file actions
52 lines (43 loc) · 1.73 KB
/
Convert.java
File metadata and controls
52 lines (43 loc) · 1.73 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
import java.util.Scanner;
/**
* Converts centimeters to feet and inches.
*/
public class Convert {
public static void main(String[] args) {
double cm;
int feet, inches, remainder;
final double CM_PER_INCH = 2.54;
final int IN_PER_FOOT = 12;
Scanner in = new Scanner(System.in);
System.out.print("Enter 1 to convert centimeters to feet and inches\n"
+ "Enter 2 to convert feet and inches to centimeters\n"
+ "Your choice: ");
int choice = in.nextInt();
if (choice == 1) {
System.out.print("Exactly how many cm? ");
cm = in.nextDouble();
if (cm < 0) {
System.out.print("Please enter a positive value for centimeters.");
} else {
inches = (int) (cm / CM_PER_INCH);
feet = inches / IN_PER_FOOT;
remainder = inches % IN_PER_FOOT;
inches = inches - (feet * IN_PER_FOOT);
System.out.printf("%.2f cm = %d ft, %d in\n", cm, feet, inches);
}
} else if (choice == 2) {
System.out.print("Enter feet: ");
feet = in.nextInt();
System.out.print("Enter inches: ");
inches = in.nextInt();
if (feet < 0 || inches < 0 || inches >= IN_PER_FOOT) {
System.out.println("Please enter valid and positive values for feet and inches.");
} else {
cm = (feet * IN_PER_FOOT + inches) * CM_PER_INCH;
System.out.printf("%d ft, %d in = %.2f cm\n", feet, inches, cm);
}
} else {
System.out.println("Invalid choice, please enter 1 or 2.");
}
}
}