我确信有更好/更简单的方法来编写此代码,但我只需要几周的时间来学习编程,目的是使用if / else if / else语句。我想要做的是获取一个值和度量单位,然后将其转换为另一个度量单位。
第一种方法:getSmallestUnit
应该将任何输入转换为英寸或毫米的最小单位。
第二种方法:getNewUnitConversion
应该接受新值并将其转换为正确的单位。
我不确定如何将变量和mm放入getNewUnitConversion
。
当我运行程序时,它只打印出常量示例的值,从1m转换为ft,打印0.00328084ft
;
import java.util.Scanner;
public class LinearConversion
{
//==================MAIN================
public static void main(String[] args)
{
Scanner newConversion = new Scanner(System.in);
System.out.print("Enter a linear unit to convert from: ");
String fromUnit = newConversion.next();
System.out.print("Enter a linear unit to convert to: ");
String toUnit = newConversion.next();
System.out.print("Enter a value: ");
double value = newConversion.nextDouble();
UnitConversion timsConversion = new UnitConversion(fromUnit, toUnit, value);
timsConversion.getSmallestUnit();
double conversion = timsConversion.getNewUnitConversion();
System.out.println(conversion);
}
}
public class UnitConversion
{
private String input;
private String output;
private double value;
private double in, ft, mi, mm, cm, m, km;
private final double inch_feet = 12;
private final double inch_miles = 63360;
private final double inch_millimeters = 25.4;
private final double inch_centimeters = 2.54;
private final double inch_meters = 0.0254;
private final double inch_kilometers = 0.0000254;
private final double millimeters_inch = 0.0393701;
private final double millimeters_feet = 0.00328084;
private final double millimeters_miles = 0.000000622;
private final double millimeter_centimeters = 10;
private final double millimeter_meters = 1000;
private final double millimeter_kilometers = 1000000;
public UnitConversion(String in, String out, double val)
{
input = in;
output = out;
value = val;
}
//Convert units to convert from (input) to its smallest form either in or mm
public double getSmallestUnit()
{
if (input.equals("mi"))
{
in = value * inch_miles;
input = "in";
}
else if (input.equals("ft"))
{
in = value * inch_feet;
input = "in";
}
else
{
in = value;
input = "in";
}
if (input.equals("km"))
{
mm = value * millimeter_kilometers;
input = "mm";
}
else if (input.equals("m"))
{
mm = value * millimeter_meters;
input = "mm";
}
else if (input.equals("cm"))
{
mm = value * millimeter_centimeters;
input = "mm";
}
else
{
mm = value;
input = "mm";
}
return 0;
}
//Convert in or mm to desired unit of measurement
public double getNewUnitConversion()
{
//convert from english standard
if (input.equals("in"))
{
if (output.equals("ft"))
{
ft = in * inch_feet;
System.out.println(ft + "ft");
}
else if (output.equals("mi"))
{
mi = in * inch_miles;
System.out.println(mi + "mi");
}
else if (output.equals("mm"))
{
mm = in * inch_millimeters;
System.out.println(mm + "mm");
}
else if (output.equals("cm"))
{
cm = in * inch_centimeters;
System.out.println(cm + "cm");
}
else if (output.equals("m"))
{
m = in * inch_meters;
System.out.println(m + "m");
}
else if (output.equals("km"))
{
km = in * inch_kilometers;
System.out.println(km + "km");
}
else if (output.equals("in"))
{
System.out.println(in + "in");
}
else
{
System.out.println("Error: Must enter linear unit of measure in, ft, mi, mm, cm, m, or km");
}
}
//convert from metric
else
{
if (output.equals("cm"))
{
cm = mm * millimeter_centimeters;
System.out.println(cm + "cm");
}
else if (output.equals("m"))
{
m = mm * millimeter_meters;
System.out.println(m + "m");
}
else if (output.equals("km"))
{
km = mm * millimeter_kilometers;
System.out.println(km + "km");
}
else if (output.equals("in"))
{
in = mm * millimeters_inch;
System.out.println(in + "in");
}
else if (output.equals("ft"))
{
ft = mm * millimeters_feet;
System.out.println(ft + "ft");
}
else if (output.equals("mi"))
{
mi = mm * millimeters_miles;
System.out.println(mi + "mi");
}
else if (output.equals("mm"))
{
System.out.println(mm + "mm");
}
else
{
System.out.println("Error: Must enter linear unit of measure in, ft, mi, mm, cm, m, or km");
}
}
return 0;
}
}
答案 0 :(得分:1)
您的问题出现在getSmallestUnit()
函数中。更改以下内容:
else if (input.equals("in"))
{
in = value;
input = "in";
}
else if (input.equals("km"))
您当前状态的代码正在检查前两个if
命令,然后强制设置in = value
和input = "in"
,因此不会传递下一个if
命令。它到达最终else
并设置mm = value
和input = mm
,因为input
既不是km
,m
,也不是cm
点。因此,您只能获得mm = value
和input = "mm
,然后将其传递到getNewUnitConversion
。如果您想要返回getNewUnitConversion
的值,则必须向每个return
添加if
语句。以下内容应涵盖在内:
public double getNewUnitConversion()
{
//convert from english standard
if (input.equals("in"))
{
if (output.equals("ft"))
{
ft = in * inch_feet;
System.out.println(ft + "ft");
return ft;
}
else if (output.equals("mi"))
{
mi = in * inch_miles;
System.out.println(mi + "mi");
return mi;
}
else if (output.equals("mm"))
{
mm = in * inch_millimeters;
System.out.println(mm + "mm");
return mm;
}
else if (output.equals("cm"))
{
cm = in * inch_centimeters;
System.out.println(cm + "cm");
return cm;
}
else if (output.equals("m"))
{
m = in * inch_meters;
System.out.println(m + "m");
return m;
}
else if (output.equals("km"))
{
km = in * inch_kilometers;
System.out.println(km + "km");
return km;
}
else if (output.equals("in"))
{
System.out.println(in + "in");
return in;
}
else
{
System.out.println("Error: Must enter linear unit of measure in, ft, mi, mm, cm, m, or km");
return 0;
}
}
//convert from metric
else
{
if (output.equals("cm"))
{
cm = mm * millimeter_centimeters;
System.out.println(cm + "cm");
return cm;
}
else if (output.equals("m"))
{
m = mm * millimeter_meters;
System.out.println(m + "m");
return m;
}
else if (output.equals("km"))
{
km = mm * millimeter_kilometers;
System.out.println(km + "km");
return km;
}
else if (output.equals("in"))
{
in = mm * millimeters_inch;
System.out.println(in + "in");
return in;
}
else if (output.equals("ft"))
{
ft = mm * millimeters_feet;
System.out.println(ft + "ft");
return ft
}
else if (output.equals("mi"))
{
mi = mm * millimeters_miles;
System.out.println(mi + "mi");
return mi;
}
else if (output.equals("mm"))
{
System.out.println(mm + "mm");
return mm;
}
else
{
System.out.println("Error: Must enter linear unit of measure in, ft, mi, mm, cm, m, or km");
return 0;
}
}
}
}
这应该有所帮助。
答案 1 :(得分:0)
您的问题是,虽然您指定了电平表,但您的代码会同时运行else
中的getSmallestUnit
块。所以1米变成1英寸,然后变成1毫米;这是0.00328英尺。
如果您将else
的第一行更改为
else if (input.equals("in"))
然后将下一个块上方的if
更改为else if
。
答案 2 :(得分:0)
首先,您在getSmallestUnit()
中遇到了问题。如果您的输入是“mi”或“fi”,则第二个if仍将执行其“else”部分:
else
{
mm = value;
input = "mm";
}
因此无论实际输入是什么,输入都将被视为“mm”。
使用单个if语句更为正确:
public double getSmallestUnit()
{
if (input.equals("mi"))
{
in = value * inch_miles;
input = "in";
}
else if (input.equals("ft"))
{
in = value * inch_feet;
input = "in";
}
else if (input.equals("km"))
{
mm = value * millimeter_kilometers;
input = "mm";
}
else if (input.equals("m"))
{
mm = value * millimeter_meters;
input = "mm";
}
else if (input.equals("cm"))
{
mm = value * millimeter_centimeters;
input = "mm";
}
else // here you can decide if your default value is "mm" or "in", but you can't have both as you tried before
{
mm = value;
input = "mm";
}
return 0;
}
接下来,您应该决定您希望返回的方法。在getSmallestUnit()
中返回0毫无意义。该方法应该有一个void返回值(即什么都不返回)。
此外,拥有所有这些私人成员 - private double in, ft, mi, mm, cm, m, km;
是没有意义的。只有一个双人成员才是最好的。 getSmallestUnit()
可以计算该值。 BTW,getSmallestUnit()
可以由构造函数调用。它根本不必是公共方法。
getNewUnitConversion()
可以将其计算值存储在本地方法变量中。它不需要将它存储在实例变量中。
完成所有建议后,代码将如下所示:
public class UnitConversion
{
private String input;
private String output;
private double smallValue;
private final double inch_feet = 12;
private final double inch_miles = 63360;
private final double inch_millimeters = 25.4;
private final double inch_centimeters = 2.54;
private final double inch_meters = 0.0254;
private final double inch_kilometers = 0.0000254;
private final double millimeters_inch = 0.0393701;
private final double millimeters_feet = 0.00328084;
private final double millimeters_miles = 0.000000622;
private final double millimeter_centimeters = 10;
private final double millimeter_meters = 1000;
private final double millimeter_kilometers = 1000000;
public UnitConversion(String in, String out, double val)
{
input = in;
output = out;
getSmallestUnit(val);
}
//Convert units to convert from (input) to its smallest form either in or mm
private void getSmallestUnit(double value)
{
if (input.equals("mi"))
{
smallValue = value * inch_miles;
input = "in";
}
else if (input.equals("ft"))
{
smallValue = value * inch_feet;
input = "in";
}
else if (input.equals("km"))
{
smallValue = value * millimeter_kilometers;
input = "mm";
}
else if (input.equals("m"))
{
smallValue = value * millimeter_meters;
input = "mm";
}
else if (input.equals("cm"))
{
smallValue = value * millimeter_centimeters;
input = "mm";
}
else
{
smallValue = value;
input = "mm";
}
}
//Convert in or mm to desired unit of measurement
public double getNewUnitConversion()
{
double outputValue = 0.0;
//convert from english standard
if (input.equals("in"))
{
if (output.equals("ft"))
{
outputValue = smallValue * inch_feet;
}
else if (output.equals("mi"))
{
outputValue = smallValue * inch_miles;
System.out.println(mi + "mi");
}
else if (output.equals("mm"))
{
outputValue = smallValue * inch_millimeters;
}
else if (output.equals("cm"))
{
outputValue = smallValue * inch_centimeters;
}
else if (output.equals("m"))
{
outputValue = smallValue * inch_meters;
}
else if (output.equals("km"))
{
outputValue = smallValue * inch_kilometers;
}
else if (output.equals("in"))
{
outputValue = smallValue;
}
else
{
System.out.println("Error: Must enter linear unit of measure in, ft, mi, mm, cm, m, or km");
}
}
//convert from metric
else
{
if (output.equals("cm"))
{
outputValue = mm * millimeter_centimeters;
}
else if (output.equals("m"))
{
outputValue = mm * millimeter_meters;
}
else if (output.equals("km"))
{
outputValue = mm * millimeter_kilometers;
}
else if (output.equals("in"))
{
outputValue = mm * millimeters_inch;
}
else if (output.equals("ft"))
{
outputValue = mm * millimeters_feet;
}
else if (output.equals("mi"))
{
outputValue = mm * millimeters_miles;
}
else if (output.equals("mm"))
{
outputValue = smallValue;
}
else
{
System.out.println("Error: Must enter linear unit of measure in, ft, mi, mm, cm, m, or km");
}
}
System.out.println(output + outputValue);
return outputValue;
}
}
当然还有很多其他事情需要改进。所有常量都应该是静态的。 getNewUnitConversion
方法可以接受输出类型作为参数,而不是在构造函数中传递它(因此您可以使用相同的实例通过多次调用此方法从一个单元转换为任何其他单元)。
答案 3 :(得分:0)
在第一个函数中返回特定值并在第二个函数中调用它。你也可以在第一个函数类型的第二个函数中添加参数,例如你需要在第二个函数中使用第一个函数的double; 功能1(双d) {/ *用双d * / return d做一些事;} “” function2(双dd) {dd = Function1(double); / 做点什么 /}