我才刚刚开始学习Java,并想创建一个程序,该程序使用switch语句将美国的距离测量值(英寸,英尺,英里)转换为公制测量值(mm,cm,m,km)。但是,我的程序有时无法区分英寸,英尺和英里之间的差异。有人可以告诉我程序中的逻辑错误吗?
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;
}
}
}
}
再次,很抱歉出现这样愚蠢的问题。
答案 0 :(得分:0)
正如@rgettman所说,您在外部convertFrom案例中缺少break语句。
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;
}
}
break; // breaks the outer switch case "in"
}
以此处的注释行所示为例。如果没有中断,它将直接进入下一个案例,即:
switch(0) {
case (0):
System.out.println("0 case");
case (1):
System.out.println("1 case");
default:
System.out.println("default case");
}
将打印出
0 case
1 case
default case