我从一个例子中获取了这段代码,但我不知道如何计算基本单位的值。 这是转换能量的代码:
public enum U_EnergyConverter implements UnitConverter {
energy_calories {
@Override
public double toBaseUnit(double amount) {
return amount;
}
},
energy_joules {
@Override
public double toBaseUnit(double amount) {
return amount * 4.19;
}
},
energy_kilocalories {
@Override
public double toBaseUnit(double amount) {
return amount * 0.001;
}
};
public abstract double toBaseUnit(double amount);
public double toUnit(double baseUnitAmount) {
return baseUnitAmount * (1 / (toBaseUnit(1)));
}
我在上面的代码中使用的金额给了我错误的结果。我不知道从哪个来源获得单位转换的确切值!!
感谢任何帮助
更新 这是一个非常有效的质量单位代码:
public enum MassConverter implements UnitConverter {
mass_g {
@Override
public double toBaseUnit(double amount) {
return amount;
}
},
mass_kg {
@Override
public double toBaseUnit(double amount) {
return amount * 1000.00;
}
},
mass_oz {
@Override
public double toBaseUnit(double amount) {
return amount * 28.3495231;
}
},
mass_lb {
@Override
public double toBaseUnit(double amount) {
return amount * 453.59237;
}
};
public abstract double toBaseUnit(double amount);
public double toUnit(double baseUnitAmount) {
return baseUnitAmount * (1 / (toBaseUnit(1)));
}
答案 0 :(得分:1)
像这样改变:
energy_kilocalories {
@Override
public double toBaseUnit(double amount) {
return amount * 1000;
}
};
给定这些单位的值(如焦耳,千卡等), toBaseUnit
应返回卡路里值。这就是正确的例子。你的反其道而行之。