我已更新我的代码。我还有问题。我确信有更好/更简单的方法来编写此代码,但我只需要几周的时间来学习编程,目的是使用if / else if / else语句。我想要做的是获取一个值和度量单位,然后将其转换为另一个度量单位。
第一种方法:getSmallestUnit应该将任何输入转换为英寸或毫米的最小单位。
第二种方法;然后getNewUnitConversion将获取新值并将其转换为正确的单位。
在此之前,我只需要使用1或2个变量。我试图返回变量以便在方法之外使用它们,但它似乎不是从mm / in计算到所需的单位。
我不知道从哪里看
public class UnitConversion
{
private String input;
private String output;
private double value;
private double temp;
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 in + mm;
}
//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;
}
else if (output.equals("mi"))
{
mi = in * inch_miles;
}
else if (output.equals("mm"))
{
mm = in * inch_millimeters;
}
else if (output.equals("cm"))
{
cm = in * inch_centimeters;
}
else if (output.equals("m"))
{
m = in * inch_meters;
}
else if (output.equals("km"))
{
km = in * inch_kilometers;
}
else if (output.equals("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 in + ft + mi + mm + cm + m + km;
}
}
这是我的主要
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);
}
}
答案 0 :(得分:0)
toString()
中的内容已关闭。 toString()
返回Object的String表示形式。您应该采用该逻辑并将其置于不同的方法中。对于toString()
,大多数IDE都可以自动生成它们。例如,在日食中你可以去Source>Generate toString()...
在你的UnitConversion构造函数中,你去:
public UnitConversion(String in, String out, double val)
{
input = in;
output = out;
value = val;
}
您可能需要查看this
关键字。它指的是当前的Object。
例如:
public UnitConversion(String input, String output, double value)
{
this.input = input;
this.output = output;
this.value = value;
}
如果要使用方法中计算的变量,则应返回该变量。没有多大意义去return 0;