TheBulgarianWolf

MetricConvertor

Jan 28th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MetricConvertor {
  4.     private static final double mm = 1000;
  5.     private static final double cm = 100;
  6.     private static final double mi = 0.000621371192;
  7.     private static final double in = 39.3700787;
  8.     private static final double km = 0.001;
  9.     private static final double feet = 3.2808399;
  10.     private static final double yards = 1.0936133;
  11.     private static final double meters = 1;
  12.  
  13.     public static void main(String[] args){
  14.         Scanner sc = new Scanner(System.in);
  15.         double mm = 1000;
  16.         double cm = 100;
  17.         double mi = 0.000621371192;
  18.         double in = 39.3700787;
  19.         double km = 0.001;
  20.         double feet = 3.2808399;
  21.         double yards = 1.0936133;
  22.         double meters = 1;
  23.         System.out.print("Enter the value of what you want to convert: ");
  24.         double number = Double.parseDouble(sc.nextLine());
  25.         System.out.print("Enter the type of the unit you enter(mm,cm,mi,in,km,ft,yd):");
  26.         String unit = sc.nextLine();
  27.         System.out.print("Enter the type of the unit you want to convert to(mm,cm,mi,in,km,ft,yd): ");
  28.         String convert = sc.nextLine();
  29.         double result = number/input(unit)*output(convert);
  30.         System.out.printf("You converted %f %s to %f %s.", number,unit,result,convert);
  31.  
  32.     }
  33.  
  34.     private static double input(String unit){
  35.          switch (unit){
  36.              case "mm":
  37.                  return mm;
  38.              case "cm":
  39.                  return cm;
  40.  
  41.              case "mi":
  42.                  return mi;
  43.  
  44.              case "km":
  45.                  return km;
  46.  
  47.              case "ft":
  48.                  return feet;
  49.  
  50.              case "yd":
  51.                  return yards;
  52.  
  53.              default:
  54.                  return meters;
  55.  
  56.          }
  57.     }
  58.  
  59.  
  60.     private static double output(String convert){
  61.         switch (convert){
  62.             case "mm":
  63.                 return mm;
  64.             case "cm":
  65.                 return cm;
  66.  
  67.             case "mi":
  68.                 return mi;
  69.  
  70.             case "km":
  71.                 return km;
  72.  
  73.             case "ft":
  74.                 return feet;
  75.  
  76.             case "yd":
  77.                 return yards;
  78.  
  79.             default:
  80.                 return meters;
  81.  
  82.         }
  83.     }
  84.  
  85. }
Add Comment
Please, Sign In to add comment