- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 JScience 库中寻找一种从一种单位类型转换为另一种单位类型的方法。给定每种类型的基本单位之间的因子,我应该能够进行转换。但 JScience 似乎对转换不太友好,只允许相同基本类型的单元之间进行转换。
基本上,我正在编写一个饮食应用程序,我需要能够在卡路里、焦耳、千焦耳、克、磅、千克等之间进行转换。常量营养素值(碳水化合物、蛋白质和脂肪)使情况变得复杂内容。
示例:
每 1 克碳水化合物 = 4 卡路里。即 4000 卡路里,或 16.736 千焦耳,或 16736 焦耳(能量的基本单位)。
Carb_Unit 定义为:16736 焦耳/克,或 16736000 焦耳/千克
给定这个基值,我应该能够从任何质量单位转换为任何能量单位。但同样,JScience 不允许这样做。有没有办法告诉转换器返回具有正确转换因子的转换器而不给出 ConversionException?
CustomUnits.java:
public class CustomUnits extends SystemOfUnits {
/**
* Holds collection of CustomUnits.
*/
private static HashSet<Unit<?>> UNITS = new HashSet<Unit<?>>();
private static <U extends Unit<?>> U customUnits(U unit) {
UNITS.add(unit);
return unit;
}
@Override
public Set<Unit<?>> getUnits() {
return Collections.unmodifiableSet(UNITS);
}
public static final Unit<Energy> KILOCALORIE = customUnits(SI.JOULE.times(4184));
public static final Unit<Energy> KILOJOULE = customUnits(SI.JOULE.times(1000));
// Food units expressed as energy
public static final Unit<Energy> CARBOHYDRATE_ENERGY = customUnits(KILOCALORIE.times(4));
public static final Unit<Energy> PROTEIN_ENERGY = customUnits(KILOCALORIE.times(4));
public static final Unit<Energy> FAT_ENERGY = customUnits(KILOCALORIE.times(9));
// Food units expressed as mass
public static final Unit<Mass> CARBOHYDRATE_MASS = customUnits(SI.GRAM);
public static final Unit<Mass> PROTEIN_MASS = customUnits(SI.GRAM);
public static final Unit<Mass> FAT_MASS = customUnits(SI.GRAM);
}
Main.java:
public static void main(String[] args) {
Amount<?> carbEnergyUnit = Amount.valueOf(1, CustomUnits.CARBOHYDRATE_ENERGY);
Amount<?> carbEnergyCalorie = carbEnergyUnit.to(CustomUnits.KILOCALORIE);
Amount<?> carbEnergyKJ = carbEnergyUnit.to(CustomUnits.KILOJOULE);
Amount<?> carbEnergyJoules = carbEnergyUnit.to(SI.JOULE);
System.out.println(carbEnergyUnit.getExactValue() + "g of carb"); // 1g of carb
System.out.println(carbEnergyCalorie.getExactValue() + " Calorie"); // 4 Calorie
System.out.println(carbEnergyKJ.getEstimatedValue() + " KiloJoules"); // 16.735999999999997 KiloJoules
System.out.println(carbEnergyJoules.getExactValue() + " Joules"); // 16736 Joules
// Exception in thread "main" javax.measure.converter.ConversionException: lb is not compatible with J*16736
UnitConverter toCarb = NonSI.POUND.getConverterTo(CustomUnits.CARBOHYDRATE_ENERGY);
double result = toCarb.convert(4);
System.out.println(result);
}
最佳答案
看起来像JScience
发现您试图转换 Unit<Mass>
到 Unit<Energy>
,默认 PhysicalModel
是禁止的.
另一种方法是创建一个新的 Quantity
适用于 FoodEnergy
的各个单位:
public static final Unit<Energy> KILOCALORIE = SI.JOULE.times(4184);
public interface FoodEnergy extends Quantity {
public final static Unit<FoodEnergy> UNIT
= (Unit<FoodEnergy>) SI.GRAM.times(KILOCALORIE);
}
private static final Unit<FoodEnergy> PROTEIN_ENERGY = FoodEnergy.UNIT.times(4);
private static final Unit<FoodEnergy> ETHANOL_ENERGY = FoodEnergy.UNIT.times(7);
…
然后您可以结合特定能源的贡献:
Amount<FoodEnergy> beer =
Amount.valueOf(2, PROTEIN_ENERGY).plus(
Amount.valueOf(14, ETHANOL_ENERGY));
System.out.println(beer.to(FoodEnergy.UNIT).getEstimatedValue() + " Calories");
打印105.99999999999997 Calories
。您可以通过转换 NonSI.POUND
来找到一磅蛋白质的卡路里含量。至SI.GRAM
:
double grams = NonSI.POUND.getConverterTo(SI.GRAM).convert(1);
Amount<FoodEnergy> pound = Amount.valueOf(grams, PROTEIN_ENERGY);
System.out.println(pound.to(FoodEnergy.UNIT).getEstimatedValue() + " Calories");
打印1814.3694799999998 Calories
。最后,您可以从 FoodEnergy.UNIT
恢复焦耳数。 :
System.out.println(FoodEnergy.UNIT.divide(SI.GRAM));
打印J*4184
,或
System.out.println(FoodEnergy.UNIT.divide(SI.GRAM).toStandardUnit().convert(1));
打印4184.0
.
关于java - 在 JScience 库中转换不同的单位类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44407365/
当我以虚构的汇率 1.05 将欧元兑换为美元时,我刚刚遇到了 JScience (4.3.1) 的奇怪行为(这意味着如果我支付 1 欧元): Currency unitMoney1 = Currenc
我想在 JScience 中定义一个新的距离单位.项目网站的“教程”部分直接指向 Javadoc,它虽然相当完整,但对我来说有点过于密集,无法理解我实际上是如何定义我自己的单元的。 你能举个例子吗?
我正在制作一个简单的物理计算器中使用 jscience。我需要计算给定一些齿轮和旋转圆柱体的转动惯量。 我更喜欢用jscience,但是jscience似乎没有转动惯量的测量方法?或者转动惯量是否以其
我们有一个系统,用户可以在其中提供任何单位的数据。数据被存储并可能转换为相同维度的任何其他单位。它基于 JScience api。 我想生成所有受支持单位的列表,以及它们拥有的所有别名。我似乎无法找到
在我正在制作的应用程序中,我需要一些物理计算(功率、力、扭矩等)。我想使用 JScience,因为它在跟踪单位方面似乎非常有用。例如,要分配速度,我会这样做: Amount vel = Amount.
我正在 JScience 库中寻找一种从一种单位类型转换为另一种单位类型的方法。给定每种类型的基本单位之间的因子,我应该能够进行转换。但 JScience 似乎对转换不太友好,只允许相同基本类型的单元
我的 Android 应用程序中有一个用例,我需要保存数量,对其执行计算,并以用户选择的单位(来自预定义列表)显示它。 JScience 完美地满足了这个需求,但由于以下错误,我无法编译 Androi
我正在尝试试用 JScience-physics 5.0-SNAPSHOT。不幸的是,我似乎无法找到进入代码的入口。 JScience网站上的例子都是4.3.1的;使用 Amount 类。从那里可以很
这是我的代码: Real AB = adj[a].get(b); System.out.println("AB " + AB); Real AC = adj[a].get(c); System.out
我尝试使用 JScience 解析 mathml 文档,但没有成功。以下是我的一段代码。 import JSci.io.*; import JSci.mathml.*; . . public
如何使用 JScience 创建涉及 vector 的符号变量/函数?创建浮点变量很容易: Variable varX = Variable.local("x"); 如何对 vector 执行相同的操
当我对 JScience Amount 对象使用 toString() 时,我得到如下结果: (7.5 ± 4.4E-16) mph 这并不糟糕,但我真的希望它输出如下内容: 7.5 miles pe
我正在寻找一种更好的方法来使用 Jscience 进行计算,但通用模式很难获得清晰的解决方案。如果定义了单位数量的单价,我需要计算 n 个单位的价格。就像, Measure unitQuant
有什么直接的方法可以将 bar 转换为 psi(磅每平方英寸)?例如将毫米转换为英寸 //mm to inches Unit mm = SI.MILLIMETER; Unit i
我有一个Vector vec在jscience中。我找不到将其标准化的功能。你能帮我正常化吗vec ? 最佳答案 类(class) org.jscience.mathematics.vector.Fl
我从 here 下载了示例代码用于使用 jscience。 但是当我使用它时,它会给出这样的信息: E/AndroidRuntime(1296): java.lang.NoClassDefFoundE
我在我的 Android 应用程序中使用 Gradle,我想使用 JScience 库依赖项。我以这种方式添加了库: dependencies { compile fileTree(dir:
我是一名优秀的程序员,十分优秀!