Я только начинаю изучать Java и хочу создать программу, которая преобразует американские измерения расстояния (дюймы, футы, мили) в метрические измерения (мм, см, м, км) с помощью операторов switch.Однако моя программа иногда не в состоянии различить разницу в дюймах, футах и милях.Может кто-нибудь показать мне логическую ошибку в моей программе?
import java.util.Scanner;
public class P3_24
{
public static void main(String [] args)
{
final double MILLIMETER_PER_INCH = 25.4;
final double CENTIMETER_PER_INCH = 2.54;
final double METER_PER_INCH = 0.0254;
final double KILOMETER_PER_INCH = 2.54E-5;
final double MILLIMETER_PER_FEET = 304.8;
final double CENTIMETER_PER_FEET = 30.48;
final double METER_PER_FEET = 0.3048;
final double KILOMETER_PER_FEET = 0.0003048;
final double MILLIMETER_PER_MILE = 1609344;
final double CENTIMETER_PER_MILE = 160934.4;
final double METER_PER_MILE = 1609.344;
final double KILOMETER_PER_MILE = 1.609344;
Scanner s = new Scanner(System.in);
System.out.println("Convert from: (in, ft, mi)");
String convertFrom = s.next().toLowerCase();
System.out.println("Convert to: (mm, cm, m, km)");
String convertTo = s.next().toLowerCase();
System.out.println("Value?");
double value = s.nextDouble();
s.close();
switch (convertFrom)
{
case ("in"):
{
switch (convertTo)
{
case ("mm"):
{
System.out.println("The value of " + value + "inches to mm is: " + (value * MILLIMETER_PER_INCH));
break;
}
case ("cm"):
{
System.out.println("The value of " + value + "inches to cm is: " + (value * CENTIMETER_PER_INCH));
break;
}
case ("m"):
{
System.out.println("The value of " + value + "inches to m is: " + (value * METER_PER_INCH));
break;
}
case ("km"):
{
System.out.println("The value of " + value + "inches to km is: " + (value * KILOMETER_PER_INCH));
break;
}
default:
{
System.out.println("output units not recognized, must be mm, cm, m, km");
break;
}
}
}
case ("ft"):
{
switch (convertTo)
{
case ("mm"):
{
System.out.println("The value of " + value + "ft to mm is: " + (value * MILLIMETER_PER_FEET));
break;
}
case ("cm"):
{
System.out.println("The value of " + value + "ft to cm is: " + (value * CENTIMETER_PER_FEET));
break;
}
case ("m"):
{
System.out.println("The value of " + value + "ft to m is: " + (value * METER_PER_FEET));
break;
}
case ("km"):
{
System.out.println("The value of " + value + "ft to km is: " + (value * KILOMETER_PER_FEET));
break;
}
default:
{
System.out.println("output units not recognized, must be mm, cm, m, km");
break;
}
}
}
case ("mi"):
{
switch (convertTo)
{
case ("mm"):
{
System.out.println("The value of " + value + "miles to mm is: " + (value * MILLIMETER_PER_MILE));
break;
}
case ("cm"):
{
System.out.println("The value of " + value + "miles to cm is: " + (value * CENTIMETER_PER_MILE));
break;
}
case ("m"):
{
System.out.println("The value of " + value + "miles to m is: " + (value * METER_PER_MILE));
break;
}
case ("km"):
{
System.out.println("The value of " + value + "miles to km is: " + (value * KILOMETER_PER_MILE));
break;
}
default:
{
System.out.println("output units not recognized, must be mm, cm, m, km");
break;
}
}
}
default:
{
System.out.println("input units not recognized, must be in, ft, mi");
break;
}
}
}
}
Опять же, извините за такой дурацкий вопрос.