Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MetricConvertor {
- private static final double mm = 1000;
- private static final double cm = 100;
- private static final double mi = 0.000621371192;
- private static final double in = 39.3700787;
- private static final double km = 0.001;
- private static final double feet = 3.2808399;
- private static final double yards = 1.0936133;
- private static final double meters = 1;
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- double mm = 1000;
- double cm = 100;
- double mi = 0.000621371192;
- double in = 39.3700787;
- double km = 0.001;
- double feet = 3.2808399;
- double yards = 1.0936133;
- double meters = 1;
- System.out.print("Enter the value of what you want to convert: ");
- double number = Double.parseDouble(sc.nextLine());
- System.out.print("Enter the type of the unit you enter(mm,cm,mi,in,km,ft,yd):");
- String unit = sc.nextLine();
- System.out.print("Enter the type of the unit you want to convert to(mm,cm,mi,in,km,ft,yd): ");
- String convert = sc.nextLine();
- double result = number/input(unit)*output(convert);
- System.out.printf("You converted %f %s to %f %s.", number,unit,result,convert);
- }
- private static double input(String unit){
- switch (unit){
- case "mm":
- return mm;
- case "cm":
- return cm;
- case "mi":
- return mi;
- case "km":
- return km;
- case "ft":
- return feet;
- case "yd":
- return yards;
- default:
- return meters;
- }
- }
- private static double output(String convert){
- switch (convert){
- case "mm":
- return mm;
- case "cm":
- return cm;
- case "mi":
- return mi;
- case "km":
- return km;
- case "ft":
- return feet;
- case "yd":
- return yards;
- default:
- return meters;
- }
- }
- }
Add Comment
Please, Sign In to add comment