我正在尝试制作单位转换计划,但我一直在接受无限的价值。我不确定我需要修复的地方,因为它不会给我错误。我只测试了毫安到毫升,以确保我正确地做到了,但我收到无限作为答案。
UnitConverter.java :
public class UnitConverter {
final double oz_TO_ml = 29.5735;
final double gal_TO_g = 3.78541;
final double lb_TO_kg = 0.453592;
final double inc_TO_mm = 25.4;//Inc is inches
final double ft_TO_cm = 30.48;
final double mi_TO_km = 1.60934;
double factor;
public UnitConverter(String unit) {
if (unit.equals("oz")) {
factor = oz_TO_ml;
} else if (unit.equals("gal")) {
factor = gal_TO_g;
} else if (unit.equals("lb")) {
factor = lb_TO_kg;
}
}
public double toOz(double amount) {
return (amount * factor);
}
public double fromOz(double amount) {
return (amount / factor);
}
public double toMl(double amount) {
return (amount * factor);
}
public double fromMl(double amount) {
return (amount / factor);
}
}
Calculator.java :
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
System.out.print("Convert to: ");
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
System.out.print("Value ");
double val = in.nextDouble();
double oz = from.toOz(val);
double converted = to.fromOz(oz);
System.out.println(val + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
示例输入:
Convert from: oz
Convert to: ml
Value 12
输出:
12.0 oz = Infinity ml
答案 0 :(得分:1)
您的UnitConverter
类构造函数只知道3个单位:oz
,gal
和lb
。如果您使用其中一个实例化它,它将正确分配因子并能够转换单位,如下所示:
public UnitConverter(String unit) {
if (unit.equals("oz")) {
factor = oz_TO_ml;
} else if (unit.equals("gal")) {
factor = gal_TO_g;
} else if (unit.equals("lb")) {
factor = lb_TO_kg;
}
}
但是,在您的Calculator类中,您有以下这一行:
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
如果您使用示例输入运行程序,则从oz
开始,到ml
。但是,如果您使用单位UnitConverter
实例化ml
,那么因素会被设置为什么?根据您的构造函数,它永远不会设置,因此它保留默认值0.0
。
稍后,你打电话给这一行:
double converted = to.fromOz(oz);
这会运行fromOz
方法
public double fromOz(double amount) {
return (amount / factor);
}
除以因子0.0
。这是Infinity输出的来源。
正如另一个答案所说,您不需要有两个UnitConverter
个对象来执行此计算。在盎司和毫升之间转换的因子是正确的,因此这个计算器代码就足够了。
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
System.out.print("Value ");
double val = in.nextDouble();
double result = from.toMl(val);
System.out.println(val + " " + fromUnit + " = " + result + " ml.");
}
}
如果您想保留当前的计算器代码,则需要在UnitConverter
构造函数中为ml(1.0
)的scalefactor添加一个条件。但是,我认为这种方法存在缺陷,因为会发生什么情况,例如,当您尝试在盎司和英寸之间进行转换时?转换毫无意义,但您的架构不会阻止它。
答案 1 :(得分:1)
public UnitConverter(String unit)
{
if (unit.equals("oz"))
{
factor = oz_TO_ml;
} else if (unit.equals("gal"))
{
factor = gal_TO_g;
} else if (unit.equals("lb"))
{ factor = lb_TO_kg;
}
}
如果你通过" ml"因子将为零
您的设计目前需要其中两个,但您实际上只需要一个" oz"拥有进行转换所需的一切。
忽略行输入代码中的toUnit,只使用fromUnit
编辑:我将向您展示另一种做事方式,它只支持一个转换来显示粗略设计。请注意,方法调用现在是静态的,因为您只需要一个实例
UnitConverter.java
public class UnitConverter
{
private static final double oz_TO_ml = 29.5735;
public static double convert(String fromType, String toType,double amount) throws IllegalArgumentException
{
if (fromType.equals("oz") && toType.equals("ml"))
{
return (amount * oz_TO_ml);
}
else
{
throw new IllegalArgumentException("The combination of converting " + fromType + " to " + toType + " is not supported");
}
}
}
Calculator.java:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
System.out.print("Convert to: ");
String toUnit = in.nextLine();
System.out.print("Value ");
double val = in.nextDouble();
System.out.println(val + " " + fromUnit + " = " + UnitConverter.convert(fromUnit,toUnit,val) + " " + toUnit);
}
}
答案 2 :(得分:1)
用一个初始化因子变量。默认情况下,java将0赋予原始double,
class UnitConvertor {
final double oz_TO_ml = 29.5735;
final double gal_TO_g = 3.78541;
final double lb_TO_kg = 0.453592;
final double inc_TO_mm = 25.4;//Inc is inches
final double ft_TO_cm = 30.48;
final double mi_TO_km = 1.60934;
double factor=1;//initialize with 1
但是,如果用户输入为' ml'我仍然不确定您使用的支票是什么。