SophiYo

MetricConverter/ ConditionalConstructions

Dec 5th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package O3_ConditionalConstructions.Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MetricConverter {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         double number = Double.parseDouble(scanner.nextLine());
  10.         String inputMetric = scanner.nextLine();
  11.         String outputMetric = scanner.nextLine();
  12.  
  13.         double convertedToMeter = 0.0;
  14.         double convertedToOutput = 0.0;
  15.  
  16.         if ("m".equalsIgnoreCase(inputMetric)) {
  17.             convertedToMeter = number;
  18.         } else if ("mm".equalsIgnoreCase(inputMetric)) {
  19.             convertedToMeter = number / 1000;
  20.         } else if ("cm".equalsIgnoreCase(inputMetric)) {
  21.             convertedToMeter = number / 100;
  22.         } else if ("mi".equalsIgnoreCase(inputMetric)) {
  23.             convertedToMeter = number / 0.000621371192;
  24.         } else if ("in".equalsIgnoreCase(inputMetric)) {
  25.             convertedToMeter = number / 39.3700787;
  26.         } else if ("km".equalsIgnoreCase(inputMetric)) {
  27.             convertedToMeter = number / 0.001;
  28.         } else if ("ft".equalsIgnoreCase(inputMetric)) {
  29.             convertedToMeter = number / 3.2808399;
  30.         } else if ("yd".equalsIgnoreCase(inputMetric)) {
  31.             convertedToMeter = number / 1.0936133;
  32.         }
  33.  
  34.  
  35.         if ("m".equalsIgnoreCase(outputMetric)) {
  36.             convertedToOutput = convertedToMeter;
  37.         } else if ("mm".equalsIgnoreCase(outputMetric)) {
  38.             convertedToOutput = convertedToMeter * 1000;
  39.         } else if ("cm".equalsIgnoreCase(outputMetric)) {
  40.             convertedToOutput = convertedToMeter * 100;
  41.         } else if ("mi".equalsIgnoreCase(outputMetric)) {
  42.             convertedToOutput = convertedToMeter * 0.000621371192;
  43.         } else if ("in".equalsIgnoreCase(outputMetric)) {
  44.             convertedToOutput = convertedToMeter * 39.3700787;
  45.         } else if ("km".equalsIgnoreCase(outputMetric)) {
  46.             convertedToOutput = convertedToMeter * 0.001;
  47.         } else if ("ft".equalsIgnoreCase(outputMetric)) {
  48.             convertedToOutput = convertedToMeter * 3.2808399;
  49.         } else if ("yd".equalsIgnoreCase(outputMetric)) {
  50.             convertedToOutput = convertedToMeter * 1.0936133;
  51.         }
  52.  
  53.         System.out.printf("%.8f", convertedToOutput);
  54.  
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment