Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double distance = Double.parseDouble(scanner.nextLine());
- String inputMetric = scanner.nextLine(),
- outputMetric = scanner.nextLine();
- if (inputMetric.equals("m")) {
- distance /= 1000;
- } else if (inputMetric.equals("cm")) {
- distance /= 100000;
- } else if (inputMetric.equals("mm")) {
- distance /= 1000000;
- } else if (inputMetric.equals("mi")) {
- distance /= 0.621371192;
- } else if (inputMetric.equals("in")) {
- distance /= 39370.0787;
- } else if (inputMetric.equals("ft")) {
- distance /= 3280.8399;
- } else if (inputMetric.equals("yd")) {
- distance /= 1093.6133;
- }
- if (outputMetric.equals("m")) {
- distance *= 1000;
- } else if (outputMetric.equals("cm")) {
- distance *= 100000;
- } else if (outputMetric.equals("mm")) {
- distance *= 1000000;
- } else if (outputMetric.equals("mi")) {
- distance *= 0.621371192;
- } else if (outputMetric.equals("in")) {
- distance *= 39370.0787;
- } else if (outputMetric.equals("ft")) {
- distance *= 3280.8399;
- } else if (outputMetric.equals("yd")) {
- distance *= 1093.6133;
- }
- System.out.printf("%f", distance);
- }
- }
- ИЛИ:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double distance = Double.parseDouble(scanner.nextLine());
- String inputMetric = scanner.nextLine(),
- outputMetric = scanner.nextLine();
- switch (inputMetric) {
- case "m" -> distance /= 1000;
- case "cm" -> distance /= 100000;
- case "mm" -> distance /= 1000000;
- case "mi" -> distance /= 0.621371192;
- case "in" -> distance /= 39370.0787;
- case "ft" -> distance /= 3280.8399;
- case "yd" -> distance /= 1093.6133;
- }
- switch (outputMetric) {
- case "m" -> distance *= 1000;
- case "cm" -> distance *= 100000;
- case "mm" -> distance *= 1000000;
- case "mi" -> distance *= 0.621371192;
- case "in" -> distance *= 39370.0787;
- case "ft" -> distance *= 3280.8399;
- case "yd" -> distance *= 1093.6133;
- }
- System.out.printf("%f", distance);
- }
- }
- Решение с тернарен оператор:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double distance = Double.parseDouble(scanner.nextLine());
- String inputMetric = scanner.nextLine(),
- outputMetric = scanner.nextLine();
- distance /=
- inputMetric.equals("m") ? 1000 :
- inputMetric.equals("cm") ? 100000 :
- inputMetric.equals("mm") ? 1000000 :
- inputMetric.equals("mi") ? 0.621371192 :
- inputMetric.equals("in") ? 39370.0787 :
- inputMetric.equals("ft") ? 3280.8399 :
- inputMetric.equals("yd") ? 1093.6133 : 1;
- distance *=
- outputMetric.equals("m") ? 1000 :
- outputMetric.equals("cm") ? 100000 :
- outputMetric.equals("mm") ? 1000000 :
- outputMetric.equals("mi") ? 0.621371192 :
- outputMetric.equals("in") ? 39370.0787 :
- outputMetric.equals("ft") ? 3280.8399 :
- outputMetric.equals("yd") ? 1093.6133 : 1;
- System.out.printf("%f", distance);
- }
- }
- Решение с колекция:
- import java.util.Map;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double distance = Double.parseDouble(scanner.nextLine());
- String inputMetric = scanner.nextLine(),
- outputMetric = scanner.nextLine();
- Map<String, Double> metricsToKM = Map.of(
- "km", 1.0,
- "m", 1000.0,
- "cm", 100000.0,
- "mm", 1000000.0,
- "mi", 0.621371192,
- "in", 39370.0787,
- "ft", 3280.8399,
- "yd", 1093.6133
- );
- System.out.printf("%f", distance / metricsToKM.get(inputMetric) * metricsToKM.get(outputMetric));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement