Create a new class named Convert and save it asĀ Convert.java.
Enter the following code and compile.
import java.util.Scanner;
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.println("Exactly how many cm? ");
cm = in.nextDouble();
inches = (int)(cm/CM_PER_INCH);
feet = inches/IN_PER_FOOT;
remainder = inches % IN_PER_FOOT;
System.out.printf("%.2f cm = %d ft, %d in\n", cm, feet, remainder);
}
}
Run the program an enter a number to convert.
> java Convert
Exactly how many cm?
22
22.00 cm = 0 ft, 8 in